-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_242.rs
58 lines (48 loc) · 1.17 KB
/
problem_242.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// https://leetcode.com/problems/valid-anagram/description/
use std::collections::HashMap;
#[allow(dead_code)]
pub fn is_anagram(s: String, t: String) -> bool {
if s.len() != t.len() {
return false;
}
if s.len() == 1 {
return s.chars().next().unwrap() == t.chars().next().unwrap();
}
let hm1 = fill_hm(&s);
let hm2 = fill_hm(&t);
for (k, v) in hm1 {
if let Some(&val) = hm2.get(&k) {
if val != v {
return false;
}
} else {
return false;
}
}
true
}
fn fill_hm(s: &str) -> HashMap<char, i32> {
let mut hm: HashMap<char, i32> = HashMap::new();
for c in s.chars() {
if hm.contains_key(&c) {
if let Some(cnt) = hm.get_mut(&c) {
*cnt += 1;
}
} else {
hm.insert(c, 1);
}
}
hm
}
#[cfg(test)]
mod tests {
use crate::problems::problem_242::is_anagram;
#[test]
fn p242_1() {
assert!(is_anagram("anagram".to_string(), "nagaram".to_string()));
}
#[test]
fn p242_2() {
assert!(!is_anagram("rat".to_string(), "car".to_string()));
}
}