1python how to find all permutations of a string

Title Description: Design a program that requires all pe...

Title Description:

Design a program that requires all permutations of a string to be output when a string is entered.
For example, the input string a B C requires the output of all the strings a B c, a C b, B a c, B C a, C a b, C B a that can be arranged by the letters a, b, C.

Method: Recursive
This paper takes the string abc as an example to introduce how to arrange the strings in full order.
(1) Fix the first character a first, then arrange the second two characters b and c in full order;
(2) Exchange the first character with the subsequent characters, that is, exchange a and b, and then arrange the latter two characters a and c in full order;
(3) Because the exchange of a and b in the second step destroys the original order of the strings, it is necessary to exchange a and b again to restore the original order, then exchange the first character with the third character (exchange A and c), then fix the first character c, and complete the arrangement of the following two characters a and b.
When the string is fully aligned, it can be solved recursively.

When using recursive solutions, be aware that:
(1) Reduce the size of the problem gradually and solve the sub-problem in the same way;
(2) Recursion must have an end condition, otherwise the program will fall into an endless loop;

Code implementation:

#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2020/2/3 9:49 # @Author : buu # @Software: PyCharm # @Blog : https://blog.csdn.net/weixin_44321080 def swap(str, i, j): # Exchange Character Array Subscripted Characters corresponding to i and j tmp = str[i] str[i] = str[j] str[j] = tmp def permutation(str, start): """ //Fully arrange the characters in the string :param str: The string to be sorted,list :param start: The first character subscript of the substring to be sorted :return: """ if str == None or start < 0: return if start == len(str) - 1: # Output current permutation string after Full Permutation print(''.join(str),end=' ') else: i = start while i < len(str): # Swap characters where start and i are located swap(str, start, i) # Fixed the first character and permutated the rest permutation(str, start + 1) # Restore characters where start and i are located swap(str, start, i) i += 1 def permutation_transe(s): str = list(s) permutation(str, 0) if __name__ == '__main__': s = 'abc' permutation_transe(s)

Result:

Algorithmic performance analysis:
Assume that the basic operands required for this method are f(n), f(n) = n*f(n-1) =...= n!
So the time complexity is O(n!);
The spatial complexity is O(1);

Extensions:

How do I remove duplicate rankings?
When there are no duplicate characters in a string, there will be no duplicate strings for all of its combinations; when there are duplicate characters in the string, such as'baa', there will be duplicate strings if you complete the arrangement according to the algorithm described above.

Ideas:
The main idea of full permutation is that from the first character, each character is exchanged with the character after it: for example, for "baa", the first and second characters of baa are exchanged to get "aba", and then the first and third characters of baa are exchanged to get "aab", since baaThe second character is equal to the third character, so the full arrangement of ABA and AAB resulting from the two exchanges is duplicated (with the first character of ABA and AAB fixed, their corresponding full arrangement is aba, aab).
So you can know that the main idea to remove repetitive permutations is:
From the first character, each character is exchanged with a non-repeating character that follows it.On the basis of recursion, just add a function to determine whether the characters are repeated or not.

Code implementation:

#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2020/2/3 10:37 # @Author : buu # @Software: PyCharm # @Blog : https://blog.csdn.net/weixin_44321080 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2020/2/3 9:49 # @Author : buu # @Software: PyCharm # @Blog : https://blog.csdn.net/weixin_44321080 def swap(str, i, j): # Exchange Character Array Subscripted Characters corresponding to i and j tmp = str[i] str[i] = str[j] str[j] = tmp def isDuplicate(str,begin,end): """ //Determine if any characters in the [begin, end] interval are equal to *end :param begin: A pointer to a character :param end: A pointer to a character :return: If there are equal characters True,else False """ i=begin while i<end: if str[i]==str[end]: return True else: i+=1 return False def permutation(str, start): """ //Fully arrange the characters in the string :param str: The string to be sorted,list :param start: The first character subscript of the substring to be sorted :return: """ if str == None or start < 0: return if start == len(str) - 1: # Output current permutation string after Full Permutation print(''.join(str),end=' ') else: i = start while i < len(str): # If there are duplicate characters, terminate the current loop and start the next one if isDuplicate(str,start,i): i+=1 continue # Swap characters where start and i are located swap(str, start, i) # Fixed the first character and permutated the rest permutation(str, start + 1) # Restore characters where start and i are located swap(str, start, i) i += 1 def permutation_transe(s): str = list(s) permutation(str, 0) if __name__ == '__main__': s = 'baa' permutation_transe(s)

Result:

end

Buoupu Published 91 original articles. Accepted 3. Visits 3399 Private letter follow

2 February 2020, 22:30 | Views: 3080

Add new comment

For adding a comment, please log in
or create account

0 comments