-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path859.txt
More file actions
26 lines (22 loc) · 690 Bytes
/
859.txt
File metadata and controls
26 lines (22 loc) · 690 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
class Solution {
public boolean buddyStrings(String A, String B) {
if(A.length()!=B.length())return false;
if(A.equals(B)){
int t1[]=new int[26];
for(int i=0;i<A.length();i++){
t1[A.charAt(i)-'a']++;
if(t1[A.charAt(i)-'a']>1)return true;
}
return false;
}
int cnt=0;
int t1[]=new int[26];
for(int i=0;i<A.length();i++){
if(A.charAt(i)!=B.charAt(i))cnt++;
t1[A.charAt(i)-'a']++;
t1[B.charAt(i)-'a']--;
}
for(int i:t1)if(i!=0)return false;
return cnt==2;
}
}