Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Using Python Permutations function on a String

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))
Comment

python all permutations of a string

>>> from itertools import permutations
>>> perms = [''.join(p) for p in permutations('stack')]
>>> perms
Comment

permutation of a string in python

# Function to find permutations of a given string
from itertools import permutations
  
def allPermutations(str):
       
     # Get all permutations of string 'ABC'
     permList = permutations(str)
  
     # print all permutations
     for perm in list(permList):
         print (''.join(perm))
        
# Driver program
if __name__ == "__main__":
    str = 'ABC'
    allPermutations(str)
Comment

Permutation in python



from itertools import permutations
from itertools import combinations
p = permutations([1,2,4]) # or  permutations([1, 2, 3], 2)
for i in p:
    print(i)
c = combinations([1,2,3],2)
for j in c:
    print(j)
    
    
Comment

permutation in a string

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

        
Comment

PREVIOUS NEXT
Code Example
Python :: how to make a timer using python 
Python :: keyboard python 
Python :: any python 
Python :: python get text of QLineEdit 
Python :: get mode using python 
Python :: while loop in python 
Python :: round to nearest multiple of 5 python 
Python :: numpy evenly spaced numbers 
Python :: make value of two tables unique django 
Python :: defaultdict python 
Python :: messages in django 
Python :: qt setfocus 
Python :: install python macos catalina 
Python :: isolationforest estimators 
Python :: list of lists to table python 
Python :: Using Lists as Queues 
Python :: python IndexError: list assignment index out of range 
Python :: create_polygon tkinter 
Python :: get unique words from pandas dataframe 
Python :: minmaxscaler transform 
Python :: q fields django Q objects 
Python :: relative text size put text cv2 
Python :: pandas read csv file 
Python :: python implementation of Min Heap 
Python :: how to import nltk 
Python :: python random choices weights 
Python :: Session in python requests 
Python :: add list of dictionaries to pandas dataframe 
Python :: list methods in python 
Python :: dict_keys to list 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =