Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e7de63b

Browse files
committedJun 11, 2023
feat: add valid anagram problem
1 parent 9c03aa3 commit e7de63b

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed
 

‎src/easy/readme.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,3 +1044,30 @@ Put the code below in main.rs and run `cargo run`
10441044
let result = easy::palindrome_linked_list::is_palindrome(head);
10451045
println!("result: {}", result);
10461046
```
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+
```

‎src/easy/valid_anagram.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

‎src/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [x] [119. Pascal's triangle II](src/easy/pascals_triangle.rs)
3333
- [x] [217. Contains duplicate](src/easy/contains_duplicate.rs)
3434
- [x] [234. Palindrome linked list](src/easy/palindrome_linked_list.rs)
35+
- [x] [242. Valid anagram](src/easy/valid_anagram.rs)
3536
- [ ] [Medium](src/medium)
3637
- [x] [2. Add two numbers](src/medium/add_two_numbers.rs)
3738
- [x] [15. Three sum](src/medium/three_sum.rs)

‎theory/categories/1.arrays_&_hashing/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
## Problems
6767

6868
- [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)
7070
- [x] [3. Two Sum](https://leetcode.com/problems/two-sum/) | Easy problem | [Solution](src/easy/two_sum.rs)
7171
- [ ] [4. Group Anagrams](https://leetcode.com/problems/group-anagrams/) | Medium Problem
7272
- [ ] [5. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | Medium Problem

0 commit comments

Comments
 (0)
Please sign in to comment.