Skip to content

Commit b033d64

Browse files
committed
solve(2351): implement solution using hash set
1 parent 3f2dd3a commit b033d64

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::collections::HashSet;
2+
3+
use crate::solutions::Solution;
4+
5+
impl Solution {
6+
pub fn repeated_character(s: String) -> char {
7+
let mut hash_set = HashSet::new();
8+
9+
for c in s.chars() {
10+
if hash_set.contains(&c) {
11+
return c;
12+
}
13+
14+
hash_set.insert(c);
15+
}
16+
17+
unreachable!()
18+
}
19+
}
20+
21+
#[test]
22+
fn test() {
23+
assert_eq!(Solution::repeated_character("abccbaacz".to_owned()), 'c');
24+
assert_eq!(Solution::repeated_character("abcdd".to_owned()), 'd');
25+
}

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod _0020_valid_parentheses;
44
pub mod _0026_remove_duplicates_from_sorted_array;
55
pub mod _0027_remove_element;
66
pub mod _1848_minimum_distance_to_the_target_element;
7+
pub mod _2351_first_letter_to_appear_twice;
78
pub mod _3046_split_the_array;
89
pub mod _3151_special_array_i;
910
pub mod _3174_clear_digits;

0 commit comments

Comments
 (0)