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 dd20beb

Browse files
committed
Solve #242
1 parent 5524223 commit dd20beb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,4 @@ mod n0233_number_of_digit_one;
204204
mod n0238_product_of_array_except_self;
205205
mod n0239_sliding_window_maximum;
206206
mod n0241_different_ways_to_add_parentheses;
207+
mod n0242_valid_anagram;

‎src/n0242_valid_anagram.rs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* [242] Valid Anagram
3+
*
4+
* Given two strings s and t , write a function to determine if t is an anagram of s.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: s = "anagram", t = "nagaram"
10+
* Output: true
11+
*
12+
*
13+
* Example 2:
14+
*
15+
*
16+
* Input: s = "rat", t = "car"
17+
* Output: false
18+
*
19+
*
20+
* Note:<br />
21+
* You may assume the string contains only lowercase alphabets.
22+
*
23+
* Follow up:<br />
24+
* What if the inputs contain unicode characters? How would you adapt your solution to such case?
25+
*
26+
*/
27+
pub struct Solution {}
28+
29+
// submission codes start here
30+
31+
impl Solution {
32+
pub fn is_anagram(s: String, t: String) -> bool {
33+
Solution::hit(s) == Solution::hit(t)
34+
}
35+
36+
fn hit(s: String) -> Vec<i32> {
37+
let mut hit = vec![0;27];
38+
for ch in s.chars() {
39+
hit[(ch as u8 - 'a' as u8) as usize] += 1;
40+
}
41+
hit
42+
}
43+
}
44+
45+
// submission codes end
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
51+
#[test]
52+
fn test_242() {
53+
assert_eq!(Solution::is_anagram("anagram".to_owned(), "nagaram".to_owned()), true);
54+
}
55+
}

0 commit comments

Comments
(0)

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