forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutationOfString.cpp
57 lines (48 loc) · 1.71 KB
/
PermutationOfString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// File name: LowerToUpper.cpp
// Author: Viet Than
// Email: [email protected], [email protected]
// Repository owner: rathoresrikant
// Repository name: HacktoberFestContribute
// Honor statement: I attest that I understand the honor code for this assignment
// and has only worked with one other person
//
// Issue: Print all permutation of a string
#include <iostream>
#include <string>
std::string StringPermutation(std::string input);
int main(){
std::string input = "abcd";
std::string undividedString = StringPermutation(input);
size_t inputSize = input.length();
size_t undividedSize = undividedString.length();
for (size_t i= 0; i< undividedSize; i=i+inputSize){
std::cout << undividedString.substr(i, inputSize)<<std::endl;
}
return 0;
}
std::string StringPermutation(std::string input){
size_t inputSize = input.length();
if (inputSize==1){
return input;
} else {
std::string total;
for (size_t i=0; i<inputSize; i++){
std:: string toPass = input;
toPass.erase(i, 1);
char first = input[i];
std::string result = StringPermutation(toPass);
size_t toPassLength, resultLength;
toPassLength = toPass.length();
resultLength = result.length();
if (resultLength==1){
total = total+first+result+result+first;
return total;
} else {
for (size_t j = 0; j<resultLength/toPassLength; j++){
total = total+first+result.substr(toPassLength*(j), toPassLength);
}
}
}
return total;
}
}