-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
buddy-strings.java
50 lines (47 loc) · 1.25 KB
/
buddy-strings.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
/**
* Problem Name : Buddy Strings
* Concept Involved : String Manipulation, Frequency Computation, Observation
*
* Execution Time : 2 ms
* Memory Consumed : 39.1 mb
*
*/
class Solution {
public boolean isDuplicate(String str){
int[] fre = new int[26];
for(int i=0; i<str.length(); i++){
int ind = str.charAt(i) - 'a';
fre[ind]++;
if(fre[ind] > 1){
return true;
}
}
return false;
}
public boolean buddyStrings(String A, String B) {
if(A.length() != B.length()){
return false;
}
int count = 0;
char ca = 'A';
char cb = 'A';
for(int i=0; i<A.length(); i++){
if(A.charAt(i) != B.charAt(i) ){
if(count == 0){
ca = A.charAt(i);
cb = B.charAt(i);
}
else if(count == 1){
if(ca != B.charAt(i) || cb != A.charAt(i)){
count++;
}
}
count++;
}
}
if(count == 2 || (count == 0 && isDuplicate(A))){
return true;
}
return false;
}
}