-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBuddyStrings.java
31 lines (30 loc) · 1002 Bytes
/
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
31
/*https://leetcode.com/problems/buddy-strings/*/
class Solution {
public boolean buddyStrings(String s, String goal) {
if (s.length() != goal.length()) return false;
int[] diffIndices = new int[2];
int[] freq = new int[26];
int index = 0, S = s.length();
char ch;
for (int i = 0; i < S; ++i)
{
++freq[s.charAt(i)-'a'];
if (s.charAt(i) != goal.charAt(i))
{
if (index == 2) return false;
diffIndices[index++] = i;
}
}
if (index == 0) if (!isDuplicatePresent(freq)) return false; else return true;
if (s.charAt(diffIndices[0]) == goal.charAt(diffIndices[1]) && s.charAt(diffIndices[1]) == goal.charAt(diffIndices[0]))
return true;
return false;
}
private boolean isDuplicatePresent(int[] freq)
{
for (int val : freq)
if (val > 1)
return true;
return false;
}
}