File tree 4 files changed +76
-1
lines changed
theory/categories/1.arrays_&_hashing 4 files changed +76
-1
lines changed Original file line number Diff line number Diff line change @@ -1044,3 +1044,30 @@ Put the code below in main.rs and run `cargo run`
1044
1044
let result = easy :: palindrome_linked_list :: is_palindrome (head );
1045
1045
println! (" result: {}" , result );
1046
1046
```
1047
+
1048
+ # 242. Valid anagram
1049
+
1050
+ ## Description
1051
+
1052
+ Given two strings s and t , write a function to determine if t is an anagram of s.
1053
+
1054
+ ## Examples
1055
+
1056
+ ``` text
1057
+ Input: s = "anagram", t = "nagaram"
1058
+
1059
+ Output: true
1060
+
1061
+ Input: s = "rat", t = "car"
1062
+
1063
+ Output: false
1064
+ ```
1065
+
1066
+ ## How to Run in main.rs
1067
+
1068
+ Put this code below in main.rs and run ` cargo run `
1069
+
1070
+ ``` rust
1071
+ let result = easy :: valid_anagram :: is_anagram (String :: from (" anagram" ), String :: from (" nagaram" ));
1072
+ println! (" result: {:?}" , result );
1073
+ ```
Original file line number Diff line number Diff line change
1
+ #![ allow( dead_code) ]
2
+ pub fn is_anagram ( s : String , t : String ) -> bool {
3
+ use std:: collections:: HashMap ;
4
+
5
+ let mut s_map: HashMap < char , i32 > = HashMap :: new ( ) ;
6
+ let mut t_map: HashMap < char , i32 > = HashMap :: new ( ) ;
7
+
8
+ for c in s. chars ( ) {
9
+ let count = s_map. entry ( c) . or_insert ( 0 ) ;
10
+ * count += 1 ;
11
+ }
12
+
13
+ for c in t. chars ( ) {
14
+ let count = t_map. entry ( c) . or_insert ( 0 ) ;
15
+ * count += 1 ;
16
+ }
17
+
18
+ s_map == t_map
19
+ }
20
+
21
+ /*
22
+ Algorithm - Hashmap
23
+ - Use a hashmap to store the frequency of each character in the first string
24
+ - Iterate through the second string and decrement the frequency of each character
25
+ - If the frequency of a character is 0, remove it from the hashmap
26
+ - If the hashmap is empty, then the strings are anagrams
27
+ - This solution is O(n) time and O(n) space
28
+
29
+ Time Complexity - O(n)
30
+ Space Complexity - O(n)
31
+ */
32
+
33
+ #[ cfg( test) ]
34
+ mod tests {
35
+ use super :: * ;
36
+
37
+ #[ test]
38
+ fn test_is_anagram ( ) {
39
+ assert_eq ! (
40
+ is_anagram( "anagram" . to_string( ) , "nagaram" . to_string( ) ) ,
41
+ true
42
+ ) ;
43
+ assert_eq ! ( is_anagram( "rat" . to_string( ) , "car" . to_string( ) ) , false ) ;
44
+
45
+ assert_eq ! ( is_anagram( "aacc" . to_string( ) , "ccac" . to_string( ) ) , false ) ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change 32
32
- [x] [ 119. Pascal's triangle II] ( src/easy/pascals_triangle.rs )
33
33
- [x] [ 217. Contains duplicate] ( src/easy/contains_duplicate.rs )
34
34
- [x] [ 234. Palindrome linked list] ( src/easy/palindrome_linked_list.rs )
35
+ - [x] [ 242. Valid anagram] ( src/easy/valid_anagram.rs )
35
36
- [ ] [ Medium] ( src/medium )
36
37
- [x] [ 2. Add two numbers] ( src/medium/add_two_numbers.rs )
37
38
- [x] [ 15. Three sum] ( src/medium/three_sum.rs )
Original file line number Diff line number Diff line change 66
66
## Problems
67
67
68
68
- [x] [ 1. Contains Duplicate] ( https://leetcode.com/problems/contains-duplicate/ ) | Easy problem | [ Solution] ( src/easy/contains_duplicate.rs )
69
- - [ ] [ 2. Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) | Easy problem
69
+ - [x ] [ 2. Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) | Easy problem | [ Solution ] ( src/easy/valid_anagram.rs )
70
70
- [x] [ 3. Two Sum] ( https://leetcode.com/problems/two-sum/ ) | Easy problem | [ Solution] ( src/easy/two_sum.rs )
71
71
- [ ] [ 4. Group Anagrams] ( https://leetcode.com/problems/group-anagrams/ ) | Medium Problem
72
72
- [ ] [ 5. Top K Frequent Elements] ( https://leetcode.com/problems/top-k-frequent-elements/ ) | Medium Problem
You can’t perform that action at this time.
0 commit comments