- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathPermutation.java
58 lines (58 loc) · 1.74 KB
/
Permutation.java
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
58
importjava.util.*;
classPermutation {
staticStringswap(Stringmystring, inti, intj) // function to swap the characters in the string
{
charch[] = mystring.toCharArray();
chartempo = ch[i];
ch[i] = ch[j];
ch[j] = tempo;
returnString.valueOf(ch);
}
staticvoidpermutation(Strings, intindex) // To permuatate the string
{
if (index == s.length() - 1) // if the index equals the length, print the string
System.out.println(s);
for (intj = index; j < s.length(); j++) {
s = swap(s, j, index); // Swap the character to manipulate the string
permutation(s, index + 1); // Send it to get the permutation
s = swap(s, j, index); // Swap back to get the original string
}
}
publicstaticvoidmain(Stringargs[]) {
ScannerI = newScanner(System.in);
System.out.println("ENTER A STRING");
Strings = I.nextLine();
System.out.println("Given Below are all the permutations of the string");
permutation(s, 0);
I.close();
}
}
// Time Complexity : N * N!
/*
//Cpp function to get the shit done, lexographically and unique elements
class Solution
{
public:
set<string> ans;
void permutate(string s, int in)
{
if(in == s.size()-1)
{
ans.insert(s);
return;
}
for(int j = in; j < s.size();j++)
{
swap(s[j],s[in]);
permutate(s, in + 1);
swap(s[j],s[in]);
}
}
vector<string>find_permutation(string S)
{
permutate(S,0);
vector<string> res(ans.begin(), ans.end());
return res;
}
};
*/