Skip to content

Commit a98d187

Browse files
committed
valid-anagram
1 parent 32d80da commit a98d187

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

valid-anagram/DaleSeo.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::collections::HashMap;
2+
3+
// TC: O(n)
4+
// SC: O(n)
5+
impl Solution {
6+
pub fn is_anagram(s: String, t: String) -> bool {
7+
let mut counter: HashMap<char, usize> = HashMap::new();
8+
9+
for ch in s.chars() {
10+
*counter.entry(ch).or_insert(0) += 1;
11+
}
12+
13+
for ch in t.chars() {
14+
counter.entry(ch)
15+
.and_modify(|cnt| *cnt -= 1)
16+
.or_insert(-1);
17+
}
18+
19+
return counter.values().all(|cnt| *cnt == 0)
20+
}
21+
}

0 commit comments

Comments
 (0)