-
Notifications
You must be signed in to change notification settings - Fork 16
/
BuddyStrings.java
30 lines (28 loc) · 1.04 KB
/
BuddyStrings.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
import java.util.Arrays;
public class BuddyStrings {
public boolean buddyStrings(String s, String goal) {
if (s.length() != goal.length()) return false;
final int[] characterFrequencies = new int[26];
final int[] need = new int[26];
int differences = 0;
char c;
for (int index = 0 ; index < s.length() ; index++) {
c = s.charAt(index);
characterFrequencies[c - 'a']++;
if (c != goal.charAt(index)) {
differences++;
need[goal.charAt(index) - 'a']++;
need[c - 'a']--;
}
}
if (differences == 1 || differences > 2) return false;
if (differences == 0 && oneCharOccurrsMoreThanOnce(characterFrequencies)) return true;
return differences == 2 && Arrays.stream(need).allMatch(val -> val == 0);
}
private boolean oneCharOccurrsMoreThanOnce(int[] frequencies) {
for (int frequency : frequencies) {
if (frequency > 1) return true;
}
return false;
}
}