-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProJinDian_1.3CompStr.java
More file actions
42 lines (36 loc) · 946 Bytes
/
Copy pathProJinDian_1.3CompStr.java
File metadata and controls
42 lines (36 loc) · 946 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
42
/*
* Compare two Strings, verify one string equal
* another one after be reformatted.
* Capital? Blank?
* IMPORTANT: they have same length
*/
// Solution One: they have same order if they are sorted by Alphabet
public String sort(String s){
char[] content = s.toCharArray();
java.util.Arrays.sort(content);
return new string(content);
}
public boolean permutation(String s, String t){
if(s.length() != t.length()){
return false;
}
return sort(s).equals(sort(t));
}
// Solution Two: They have same number of char, so we can calculate it.
public boolean permutation(String s, String t){
if(s.length != t.length()){
return false;
}
int[] letters = new int[256]; //ASCII
char[] s_array = s.toCharArray();
for(char c : s_array){// Calculate the number of each char in String
letters[c]++;
}
for(int i = 0; i < t.length(); i++){
int c = (int) t.charAt(i);
if(--letters[c] < 0){
return false;
}
}
return true;
}