Question : Using Python Permutations function on a String
Answered by : softhunt
from itertools import permutations
string="SOFT"
a=permutations(string)
for i in list(a): # join all the letters of the list to make a string print("".join(i))
Source : https://softhunt.net/understanding-permutations-python-function-with-examples/ | Last Update : Sun, 17 Apr 22
Question : python all permutations of a string
Answered by : worrisome-wallaby-ljn7zl235m9y
>>> from itertools import permutations
>>> perms = [''.join(p) for p in permutations('stack')]
>>> perms
Source : https://stackoverflow.com/questions/8306654/finding-all-possible-permutations-of-a-given-string-in-python | Last Update : Sun, 15 Nov 20
Question : find all permutations of a string
Answered by : homeless-hornet-1xv5byeae0sc
void permute(string a, int l, int r)
{ // Base case if (l == r) cout<<a<<endl; else { // Permutations made for (int i = l; i <= r; i++) { // Swapping done swap(a[l], a[i]); // Recursion called permute(a, l+1, r); //backtrack swap(a[l], a[i]); } }
}
Source : | Last Update : Fri, 08 May 20
Question : get all permutations of string
Answered by : impossible-impala-2kf2sz6ngusb
# get all permutations of string
import itertools
for p in itertools.permutations('123'): print(p) # ( ' 1 ', ' 2 ', ' 3 ') ( ' 1 ' , ' 3 ', ' 2 ' ) ( ' 2 ', ' 1 ', ' 3 ' )
Source : | Last Update : Mon, 28 Mar 22
Question : how to print all permutations of a string
Answered by : aryan-khare
void permutation(string s)
{ sort(s.begin(),s.end()); do{ cout << s << " "; } while(next_permutation(s.begin(),s.end()); // std::next_permutation cout << endl;
}
Source : | Last Update : Tue, 06 Oct 20
Question : permutation in a string
Answered by : upset-unicorn-9oiadusi5ykr
import collections
class Solution: def checkInclusion(self, s1, s2): """ :type s1: str :type s2: str :rtype: bool """ ''' Apprach 3: Just changing the collections.Counter to collections.defaultdict improved the speed 94.36%, Nice. :) ''' ctr1 = collections.defaultdict(int) ctr2 = collections.defaultdict(int) for x in s1: ctr1[x] += 1 for x in s2[:len(s1)]: ctr2[x] += 1 i = 0; j = len(s1) while j < len(s2): if ctr2 == ctr1: return True ctr2[s2[i]] -= 1 if ctr2[s2[i]] < 1: ctr2.pop(s2[i]); ctr2[s2[j]] = ctr2.get(s2[j], 0) + 1 i += 1; j += 1 return ctr2 == ctr1 ''' Approach 2: Use the same counter but much more effeciently 63% beated, okay not bad.. two pointers i(front) j(last), delete and add accordingly and check for the counts # Code Below ''' ctr1 = collections.Counter(s1) ctr2 = collections.Counter(s2[:len(s1)]) i = 0; j = len(s1) while j < len(s2): if ctr2 == ctr1: return True ctr2[s2[i]] -= 1 if ctr2[s2[i]] < 1: ctr2.pop(s2[i]); ctr2[s2[j]] = ctr2.get(s2[j], 0) + 1 i += 1; j += 1 return ctr2 == ctr1 ''' 1. Approach 1 Things i Missed: - Had the len(s2) - len(s1) + 1: forgot to add the 1 in the if condition Ultra easy solution Slice and check for the equality 5% beat, yuck !! # Code below ''' ctr1 = collections.Counter(s1) i = 0 while i < len(s2) - len(s1) + 1: if s2[i] in ctr1: ctr2 = collections.Counter(s2[i: i+len(s1)]) if ctr1 == ctr2: return True i += 1 return False
Source : https://leetcode.com/problems/permutation-in-string/discuss/102594/Python-Simple-with-Explanation | Last Update : Tue, 16 Nov 21