Skip to content

Commit d00ccb6

Browse files
authored
Update Determine if Two Strings Are Close.java
1 parent f7fd40e commit d00ccb6

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
class Solution {
2-
public boolean closeStrings(String word1, String word2) {
3-
if (word1.length() != word2.length()) {
4-
return false;
2+
public boolean closeStrings(String word1, String word2) {
3+
if (word1.length() != word2.length()) {
4+
return false;
5+
}
6+
int[] frequencyOne = getFrequencyArray(word1);
7+
int[] frequencyTwo = getFrequencyArray(word2);
8+
for (int i = 0; i < 26; i++) {
9+
if (frequencyOne[i] == 0 && frequencyTwo[i] != 0) {
10+
return false;
11+
}
12+
}
13+
Arrays.sort(frequencyOne);
14+
Arrays.sort(frequencyTwo);
15+
for (int i = 0; i < 26; i++) {
16+
if (frequencyOne[i] != frequencyTwo[i]) {
17+
return false;
18+
}
19+
}
20+
return true;
521
}
6-
int[] frequencyOne = getFrequencyArray(word1);
7-
int[] frequencyTwo = getFrequencyArray(word2);
8-
for (int i = 0; i < frequencyOne.length; i++) {
9-
if (frequencyOne[i] == 0 && frequencyTwo[i] != 0) {
10-
return false;
11-
}
22+
23+
private static int[] getFrequencyArray(String s) {
24+
int[] frequency = new int[26];
25+
for (int i = 0; i < s.length(); i++) {
26+
frequency[s.charAt(i) - 'a']++;
27+
}
28+
return frequency;
1229
}
13-
Arrays.sort(frequencyOne);
14-
Arrays.sort(frequencyTwo);
15-
for (int i = 0; i < frequencyOne.length; i++) {
16-
if (frequencyOne[i] != frequencyTwo[i]) {
17-
return false;
18-
}
19-
}
20-
return true;
21-
}
22-
23-
private int[] getFrequencyArray(String s) {
24-
int[] frequency = new int[26];
25-
for (char c : s.toCharArray()) {
26-
frequency[c - 'a']++;
27-
}
28-
return frequency;
29-
}
3030
}

0 commit comments

Comments
 (0)