-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_anagram.rs
50 lines (41 loc) · 1.18 KB
/
valid_anagram.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
#![allow(dead_code)]
pub fn is_anagram(s: String, t: String) -> bool {
use std::collections::HashMap;
let mut map: HashMap<char, i32> = HashMap::new();
for c in s.chars() {
let count = map.entry(c).or_insert(0);
*count += 1;
}
for c in t.chars() {
let count = map.entry(c).or_insert(0);
*count -= 1;
}
map.values().all(|&x| x == 0)
}
/*
Algorithm - Hashmap
- Create a hashmap
- Iterate through the first string
- Insert each character into the hashmap with a value of 1
- Iterate through the second string
- Insert each character into the hashmap with a value of -1
- Iterate through the hashmap values
- If any value is not 0
- Return false
- Return true
Time Complexity - O(n)
Space Complexity - O(n)
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_anagram() {
assert_eq!(
is_anagram("anagram".to_string(), "nagaram".to_string()),
true
);
assert_eq!(is_anagram("rat".to_string(), "car".to_string()), false);
assert_eq!(is_anagram("aacc".to_string(), "ccac".to_string()), false);
}
}