Offer 27: Print out all the permutations of the characters in the string in dictionary order

Keywords: C++

1 Topic Description

Enter a string and print out all the permutations of the characters in the string in dictionary order. For example, if you input the string a B c, you print out all the strings abc, a C b, bac, B C a, cab and cba that can be arranged by the characters a, b, C.

Input Description:

Enter a string of no more than 9 lengths (possibly duplicated characters), and the characters include only upper and lower case letters.

2 Thoughts and Methods

Fixed the first character, and recursively acquired various string combinations after the first character; then exchanged the first character with each character after it, and recursively acquired its string combinations; each recursion ended at the last bit, and the recursive cycle started with the second character of each substring, and then continued to process the substring.

3 C++ Core Code

 1 class Solution {
 2 public:
 3     vector<string> result;
 4     vector<string> Permutation(string str) {
 5         if(str.length()==0)
 6             return result;
 7         permutation1(str,0);
 8         sort(result.begin(),result.end());
 9         return result;
10     }
11     void permutation1(string str,int begin){
12         if(begin==str.length())
13         {
14             result.push_back(str);
15             return;
16         }
17         for(int i = begin;str[i]!='\0';i++)
18         {
19             if(i!=begin&&str[begin]==str[i])
20                 continue;
21             swap(str[begin],str[i]);
22             permutation1(str,begin+1);
23             swap(str[begin],str[i]);
24         }
25     }
26 };

4 C++ Complete Code

 1 #include <stdio.h>
 2 #include <vector>
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 void swap(char &a, char &b) {
 9     char temp = a;
10     a = b;
11     b = temp;
12 }
13 void permcore(string list, int low, int high, vector<string>& res) {
14     if (low == high &&
15         find(res.begin(), res.end(), list) == res.end()) {  //Duplicate removal 
16         res.push_back(list);
17     }
18     else {
19         for (int i = low; i <= high; i++) {//Each element exchanges with the first element
20             if (i == low || list[i] != list[low]) {    //Duplicate removal
21                 swap(list[i], list[low]);
22                 permcore(list, low + 1, high, res); //After exchange,Get a subsequence,Use function perm Get Full Permutation of subsequences
23                 swap(list[i], list[low]);//Last,Exchange elements back,Restore,Then swap another element
24             }
25         }
26     }
27 }
28 
29 vector<string> perm(string str)
30 {
31     vector<string> res;
32     if (!str.empty())
33         permcore(str, 0, str.size() - 1, res);
34     return res;
35 }
36 
37 int main()
38 {
39     vector<string> res;
40     string stdstr = "abb";
41     res = perm(stdstr);
42     for (auto s : res)
43         cout << s << endl;
44     cout << endl;
45 
46     string stdstr2 = "aab";
47     res = perm(stdstr2);
48     for (auto s : res)
49         cout << s << endl;
50     cout << endl;
51 
52     system("pause");
53     return 0;
54 }

Reference material

https://blog.csdn.net/JarvisKao/article/details/76999473

Posted by kasitzboym on Sat, 05 Oct 2019 02:13:15 -0700