File tree Expand file tree Collapse file tree
1-easy/0242_valid_anagram Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " valid_anagram"
3+ version = " 0.1.0"
4+ edition = " 2024"
5+
6+ [dependencies ]
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments