-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPermutation.java
More file actions
41 lines (36 loc) · 929 Bytes
/
StringPermutation.java
File metadata and controls
41 lines (36 loc) · 929 Bytes
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
package OCJP;
import java.util.*;
class StringPerm{
public ArrayList<Character> compute(String s){
// Convert the string to character array
char k[] = s.toCharArray();
// Create a generic ArrayList
ArrayList<Character> a = new ArrayList<Character>();
// Add the character elements to the ArrayList
for(char val:k)
a.add(val);
// Sort the individual characters
Collections.sort(a);
return a;
}
public boolean permutation(String s1,String s2){
if(s1.length() != s2.length())
return false;
ArrayList<Character> a1 = compute(s1);
ArrayList<Character> a2 = compute(s2);
if(a1.equals(a2))
return true;
return false;
}
}
public class StringPermutation {
public static void main(String[] args) {
StringPerm p = new StringPerm();
boolean b;
b = p.permutation("hello", "lloeh");
if(b)
System.out.println("Permutation");
else
System.out.println("Not a permutation");
}
}