-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTwo Strings Are Anagrams.java
executable file
·103 lines (86 loc) · 2.64 KB
/
Two Strings Are Anagrams.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
E
方法1:char ascii 用count[256]
坑:不要想象这个是个26letter lowercase. may not be true.
方法2: 若是其他字符encoding, 而不只是utf16-encoding (java char)?
那么就继续用string去做
```
/*
Write a method anagram(s,t) to decide if two strings are anagrams or not.
Example
Given s="abcd", t="dcab", return true.
Challenge
O(n) time, O(1) extra space
Tags Expand
String Cracking The Coding Interview
*/
/*
Recap 12.09.2015
O(n) time: cannot Arrays.sort()
O(1) extra space: cannot convert to char array. But we can use a count[256] to count the occuranceo of letters.
do 1 for loop:
+ occurance index
- occurance index
border cases:
null: false.
both length() == 0, true
length() not equal: false
don't assume all lower case.
there are ' ' space, so it can't be just 26 letters. make it 256
*/
public class Solution {
public boolean anagram(String s, String t) {
if (s == null || t == null) {
return false;
} else if (s.length() != t.length()) {
return false;
} else if (s.length() == 0 && t.length() == 0) {
return true;
}
int[] count = new int[256];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i)]++;
count[t.charAt(i)]--;
}
for (int num : count) {
if (num != 0) {
return false;
}
}
return true;
}
};
/*
What if it's not just ascii code, maybe uni-code?
Then the character (utf16-encoding) may not be enough. So we use String here.
*/
//check length. compare
public class Solution {
public boolean isAnagram(String s, String t) {
if (s == null || t == null || s.length() != t.length()) {
return false;
}
if (s.equals(t)) {
return true;
}
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < s.length(); i++) {
String ss = s.substring(i, i + 1);
String tt = t.substring(i, i + 1);
if (!map.containsKey(ss)) {
map.put(ss, 0);
}
map.put(ss, map.get(ss) + 1);
if (!map.containsKey(tt)) {
map.put(tt, 0);
}
map.put(tt, map.get(tt) - 1);
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() != 0) {
return false;
}
}
return true;
}
}
```