Given a string, find all of its permutations
2 posters
Page 1 of 1
Given a string, find all of its permutations
1____void find_permutations(string s, int i, int n, bool check_duplicate) {
2__________if (i >= n) {
3________________cout << s << " ";
4__________} else {
5________________bool ch[256];
6________________for (int j=0; j<256; j++) ch[j] = false;
7________________for (int j=i; j<n; j++) {
8______________________if (!ch[s[j]] || !check_duplicate) {
9____________________________ch[s[j]] = true;
10___________________________swap(s[i], s[j]);
11___________________________find_permutations(s, i+1, n, check_duplicate);
12___________________________swap(s[i], s[j]);
13_____________________}____________
14_______________}
15_________}
16___}
2__________if (i >= n) {
3________________cout << s << " ";
4__________} else {
5________________bool ch[256];
6________________for (int j=0; j<256; j++) ch[j] = false;
7________________for (int j=i; j<n; j++) {
8______________________if (!ch[s[j]] || !check_duplicate) {
9____________________________ch[s[j]] = true;
10___________________________swap(s[i], s[j]);
11___________________________find_permutations(s, i+1, n, check_duplicate);
12___________________________swap(s[i], s[j]);
13_____________________}____________
14_______________}
15_________}
16___}
viterbi- Posts : 32
Join date : 2011-09-03
Re: Given a string, find all of its permutations
Hi I'm not sure about the arguments in the method
does i begin from 0 and n is the length of the string s?
Also what is the boolean check_duplicate?
does i begin from 0 and n is the length of the string s?
Also what is the boolean check_duplicate?
viterbi wrote:1____void find_permutations(string s, int i, int n, bool check_duplicate) {
2__________if (i >= n) {
3________________cout << s << " ";
4__________} else {
5________________bool ch[256];
6________________for (int j=0; j<256; j++) ch[j] = false;
7________________for (int j=i; j<n; j++) {
8______________________if (!ch[s[j]] || !check_duplicate) {
9____________________________ch[s[j]] = true;
10___________________________swap(s[i], s[j]);
11___________________________find_permutations(s, i+1, n, check_duplicate);
12___________________________swap(s[i], s[j]);
13_____________________}____________
14_______________}
15_________}
16___}
skyboard- Posts : 31
Join date : 2011-09-03
Re: Given a string, find all of its permutations
Sorry, I should write some explanation. Yes, you call it with i=0 and n is the length of the string. check_duplicate means to out put the same string twice if there is duplicate characters.
For example, "aab", if check duplicate, all permutations are aab, aba, baa,
if not, then aab, aab, aba, aba, baa, baa.
For example, "aab", if check duplicate, all permutations are aab, aba, baa,
if not, then aab, aab, aba, aba, baa, baa.
skyboard wrote:Hi I'm not sure about the arguments in the method
does i begin from 0 and n is the length of the string s?
Also what is the boolean check_duplicate?viterbi wrote:1____void find_permutations(string s, int i, int n, bool check_duplicate) {
2__________if (i >= n) {
3________________cout << s << " ";
4__________} else {
5________________bool ch[256];
6________________for (int j=0; j<256; j++) ch[j] = false;
7________________for (int j=i; j<n; j++) {
8______________________if (!ch[s[j]] || !check_duplicate) {
9____________________________ch[s[j]] = true;
10___________________________swap(s[i], s[j]);
11___________________________find_permutations(s, i+1, n, check_duplicate);
12___________________________swap(s[i], s[j]);
13_____________________}____________
14_______________}
15_________}
16___}
viterbi- Posts : 32
Join date : 2011-09-03
Similar topics
» Permutations of a String
» How to find the longest palindrome in a string
» Find duplicate words in a string
» Given a sorted array find all possible |ai - aj| where ai,aj belongs to Array A. n^2 is obvious. Find a solution in O(N).
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» How to find the longest palindrome in a string
» Find duplicate words in a string
» Given a sorted array find all possible |ai - aj| where ai,aj belongs to Array A. n^2 is obvious. Find a solution in O(N).
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum