Check if two strings are permutation of each other | Python3 function to check whether two strings are Permutations of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array
def arePermutation ( str1 , str2 ) : NEW_LINE INDENT count = [ 0 for i in range ( NO_OF_CHARS ) ] NEW_LINE i = 0 NEW_LINE while ( str1 [ i ] and str2 [ i ] ) : NEW_LINE INDENT count [ str1 [ i ] ] += 1 NEW_LINE count [ str2 [ i ] ] -= 1 NEW_LINE DEDENT if ( str1 [ i ] or str2 [ i ] ) : NEW_LINE return False ; NEW_LINE for i in range ( NO_OF_CHARS ) : NEW_LINE INDENT if ( count [ i ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT