Skip to content

Commit fed544e

Browse files
committed
Add implementation for valid anagram check
1 parent 6556dd0 commit fed544e

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "valid_anagram"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// # 0242 Given two strings s and t, return true if t is an anagram of s, and false otherwise.
2+
3+
fn main() {
4+
let s = String::from("hello");
5+
let t = String::from("leloh");
6+
println!("{}", Solution::is_anagram(s, t));
7+
}
8+
9+
use std::collections::HashMap;
10+
11+
struct Solution;
12+
13+
impl Solution {
14+
pub fn is_anagram(s: String, t: String) -> bool {
15+
let mut s_hashmap: HashMap<char, i32> = HashMap::new();
16+
let mut t_hashmap: HashMap<char, i32> = HashMap::new();
17+
18+
Solution::numerate(&s, &mut s_hashmap);
19+
Solution::numerate(&t, &mut t_hashmap);
20+
21+
s_hashmap == t_hashmap
22+
}
23+
24+
fn numerate(st: &String, hm: &mut HashMap<char, i32>) {
25+
let s1 = st.chars().enumerate();
26+
s1.for_each(|ch| {
27+
let key = ch.1;
28+
let value = hm.get(&key);
29+
30+
match value {
31+
Some(val) => {
32+
hm.insert(key, *val + 1);
33+
}
34+
None => {
35+
hm.insert(key, 1);
36+
}
37+
}
38+
});
39+
}
40+
}

0 commit comments

Comments
 (0)