Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 659a6fd

Browse files
add solution in rust using HashMap: 242. Valid Anagram
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent 0cdb3c4 commit 659a6fd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎src/0201-0300/242 - Valid Anagram/valid_anagram.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1+
use std::collections::HashMap;
2+
13
impl Solution {
4+
// using hashmap
5+
pub fn is_anagram(s: String, t: String) -> bool {
6+
if s.len() != t.len() {
7+
return false;
8+
}
9+
10+
let s_map = s.chars().fold(HashMap::new(), |mut map, c| {
11+
// *map.entry(c).or_insert(0) = map.get(&c).unwrap_or(&0) + 1;
12+
// same as above
13+
*map.entry(c).or_insert(0) += 1;
14+
map
15+
});
16+
17+
let t_map = t.chars().fold(HashMap::new(), |mut map, c| {
18+
*map.entry(c).or_insert(0) += 1;
19+
map
20+
});
21+
22+
s_map
23+
.iter()
24+
.all(|(key, value)| t_map.get(key) == Some(value))
25+
}
26+
227
pub fn is_anagram(s: String, t: String) -> bool {
328
if (s.len() != t.len()) {
429
return false;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /