forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompare2StringsAreClose.java
More file actions
38 lines (27 loc) · 850 Bytes
/
Compare2StringsAreClose.java
File metadata and controls
38 lines (27 loc) · 850 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
class Solution {
// TC : O(max Len(word1, word2)) O(max(l1, l2))
// SC: O(1)
public boolean closeStrings(String word1, String word2) {
int[] freq1 = new int[26];
int[] freq2 = new int[26];
Set<Character> set1 = new HashSet<>();
Set<Character> set2 = new HashSet<>();
for(char c : word1.toCharArray()){
freq1[c -'a']++;
set1.add(c);
}
for(char c : word2.toCharArray()){
freq2[c -'a']++;
set2.add(c);
}
Set<Integer> freqSet1 = new HashSet<>();
Set<Integer> freqSet2 = new HashSet<>();
for(int freq : freq1){
freqSet1.add(freq);
}
for(int freq : freq2){
freqSet2.add(freq);
}
return set1.equals(set2) && freqSet1.equals(freqSet2);
}
}