Skip to content

Commit dddf826

Browse files
committed
p1790
1 parent 02e98dc commit dddf826

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

.DS_Store

0 Bytes
Binary file not shown.

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
mod p1800;
1+
mod p1790;
22

33
pub fn main() {
4-
p1800::run();
4+
p1790::run();
55
}

src/p1790.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::collections::HashSet;
2+
3+
pub fn run() {
4+
for i in [
5+
("bank", "kanb"),
6+
("attack", "defend"),
7+
("kelb", "kelb"),
8+
("aa", "ac"),
9+
("aa", "bb")
10+
] {
11+
println!("{}", are_almost_equal(i.0.to_string(), i.1.to_string()));
12+
}
13+
}
14+
15+
pub fn are_almost_equal(s1: String, s2: String) -> bool {
16+
let mut s1 = s1.chars();
17+
let mut s2 = s2.chars();
18+
19+
let mut differences_a = HashSet::new();
20+
let mut differences_b = HashSet::new();
21+
22+
let mut count = 0;
23+
24+
while let Some(a) = s1.next() {
25+
let b = s2.next().unwrap();
26+
27+
if a != b {
28+
differences_a.insert(a);
29+
differences_b.insert(b);
30+
31+
count += 1;
32+
if count >= 3 {
33+
return false;
34+
}
35+
}
36+
}
37+
38+
(differences_a == differences_b) &&
39+
((count == 0) || (count == 2))
40+
}

0 commit comments

Comments
 (0)