forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0859.rs
32 lines (30 loc) · 764 Bytes
/
0859.rs
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
impl Solution {
pub fn buddy_strings(s: String, goal: String) -> bool {
let n = s.len();
if n != goal.len() {
return false;
}
let mut t = 0;
let mut ok = false;
let mut a = vec![0; 26];
let mut b = vec![0; 26];
let s = s.as_bytes();
let goal = goal.as_bytes();
for i in 0..n {
a[(s[i] - b'a') as usize] += 1;
b[(goal[i] - b'a') as usize] += 1;
if s[i] != goal[i] {
t += 1;
}
}
for i in 0..26 {
if a[i] != b[i] {
return false;
}
if a[i] > 1 {
ok = true;
}
}
t == 2 || (t == 0 && ok)
}
}