forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid-anagram.java
93 lines (73 loc) · 2.45 KB
/
valid-anagram.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
/**
* Given two strings s and t , write a function to determine if t is an anagram of s.
*
* Note:
* You may assume the string contains only lowercase alphabets.
* Follow up:
* What if the inputs contain unicode characters? How would you adapt your solution to such case?
*
* Works for any ASCII characters - 128 ASCII chars are there.
* for unicode chars - 256 chars are there.
*/
class Solution {
// 1. Brute force approach
// Time Complexity: O(n2) | Space Complexity: O(1)
public boolean isAnagramUsingBruteForce(String s, String t) {
if (s.length() != t.length()){
return false;
}
else if(s.isEmpty()){
return true;
}
boolean isAnagram = false;
boolean [] visited = new boolean[t.length()];
for(int i=0; i<s.length(); i++){
isAnagram = false;
for(int j=0; j<t.length(); j++){
if(s.charAt(i) == t.charAt(j) && !visited[j]){
isAnagram = true;
visited[j] = true;
break;
}
}
if(!isAnagram){
return false;
}
}
return isAnagram;
}
// 2. Sorting approach
// Time Complexity: O(nlogn) | Space Complexity: O(n)
public boolean isAnagramUsingSorting(String s, String t) {
// length check
if (s.length() != t.length()){
return false;
}
else if(s.isEmpty()){ // empty string check
return true;
}
char [] ch1 = s.toCharArray();
char [] ch2 = t.toCharArray();
// sort both arrays
Arrays.sort(ch1);
Arrays.sort(ch2);
return Arrays.equals(ch1, ch2); // compare each character at same position or index
}
// 3. Efficient approach using hashing
// Time: O(n) | Space: O(1) [because the table's size stays constant no matter how large n is]
public boolean isAnagram(String s, String t) {
int [] charCount = new int[128];
for (int i=0; i<s.length(); i++){
charCount[s.charAt(i)]++;
}
for (int i=0; i<t.length(); i++){
charCount[t.charAt(i)]--;
}
for (int i=0;i<128;i++){
if(charCount[i] != 0){
return false;
}
}
return true;
}
}