-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathis_anagram.rs
39 lines (32 loc) · 908 Bytes
/
is_anagram.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 242. Valid Anagram, Easy
// https://leetcode.com/problems/valid-anagram/
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
if s.len() != t.len() {
return false;
}
let mut s_chars: Vec<char> = s.chars().collect();
let mut t_chars: Vec<char> = t.chars().collect();
s_chars.sort_unstable();
t_chars.sort_unstable();
for i in 0..s_chars.len() {
if (s_chars[i] as u8) != (t_chars[i] as u8) {
return false;
}
}
true
}
}
struct Solution {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_anagram() {
assert_eq!(Solution::is_anagram("anagram".to_string(), "nagaram".to_string()), true);
}
#[test]
fn test_is_anagram2() {
assert_eq!(Solution::is_anagram("rat".to_string(), "car".to_string()), false);
}
}