Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d1fc252

Browse files
fix find_the_difference (#30)
1 parent 2aa06f8 commit d1fc252

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ cargo test
6666
0318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/maximum-product-of-word-lengths.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py) <br>------</br> [Rust](./kamyu104/src/maximum_product_of_word_lengths.rs) | _O(n)_ ~ _O(n^2)_ | _O(n)_ | Medium || Bit Manipulation, Counting Sort, Pruning|
6767
0342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/power-of-four.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/power-of-four.py) <br>------</br> [Rust](./kamyu104/src/power_of_four.rs) | _O(1)_ | _O(1)_ | Easy | |
6868
0371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/sum-of-two-integers.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/sum-of-two-integers.py) <br>------</br> [Rust](./kamyu104/src/sum_of_two_integers.rs) | _O(1)_ | _O(1)_ | Easy | LintCode |
69-
0389 | [~~Find the Difference~~](https://leetcode.com/problems/find-the-difference/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/find-the-difference.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/find-the-difference.py) <br>------</br> [Rust](./kamyu104/src/find_the_difference.rs) | _O(n)_ | _O(1)_ | Easy | |
69+
0389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/find-the-difference.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/find-the-difference.py) <br>------</br> [Rust](./kamyu104/src/find_the_difference.rs) | _O(n)_ | _O(1)_ | Easy | |
7070
0393 | [UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/utf-8-validation.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/utf-8-validation.py) <br>------</br> [Rust](./kamyu104/src/utf_8_validation.rs) | _O(n)_ | _O(1)_ | Medium | |
7171

7272
<!--

kamyu104/src/find_the_difference.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,74 @@
1-
// pub struct Solution {}
2-
// impl Solution {
1+
use std::collections::HashSet;
2+
3+
// Time: O(n)
4+
// Space: O(1)
5+
6+
// pub struct Solution1 {}
7+
// impl Solution1 {
8+
// pub fn find_the_difference(s: String, t: String) -> char {
9+
// 'e'
10+
// }
11+
// }
12+
13+
// pub struct Solution2 {}
14+
// impl Solution2 {
15+
// pub fn find_the_difference(s: String, t: String) -> char {
16+
// 'e'
17+
// }
18+
// }
19+
20+
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/find-the-difference.py#L18
21+
pub struct Solution3 {}
22+
impl Solution3 {
23+
pub fn find_the_difference(s: String, t: String) -> char {
24+
let s_chars: HashSet<char> = s.chars().collect();
25+
let mut t_chars: HashSet<char> = t.chars().collect();
26+
s_chars.iter().for_each(|c| {
27+
t_chars.remove(c);
28+
});
29+
t_chars.into_iter().collect::<Vec<char>>()[0]
30+
}
31+
}
32+
33+
// pub struct Solution4 {}
34+
// impl Solution4 {
35+
// pub fn find_the_difference(s: String, t: String) -> char {
36+
// 'e'
37+
// }
38+
// }
39+
40+
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/find-the-difference.py#L33
41+
pub struct Solution5 {}
42+
impl Solution5 {
43+
pub fn find_the_difference(s: String, t: String) -> char {
44+
let s_chars: HashSet<char> = s.chars().collect();
45+
let t_chars: HashSet<char> = t.chars().collect();
46+
// or https://doc.rust-lang.org/std/collections/struct.HashSet.html#method.symmetric_difference
47+
let diff: HashSet<&char> = t_chars.difference(&s_chars).collect();
48+
*(diff.into_iter().collect::<Vec<&char>>()[0])
49+
}
50+
}
51+
52+
// pub struct Solution6 {}
53+
// impl Solution6 {
354
// pub fn find_the_difference(s: String, t: String) -> char {
455
// 'e'
556
// }
657
// }
758

859
#[cfg(test)]
960
mod tests {
10-
// Note this useful idiom: importing names from outer (for mod tests) scope.
1161
use super::*;
1262

1363
#[test]
1464
fn test_find_the_difference() {
15-
// assert_eq!(Solution::find_the_difference("abcd".to_string(), "abcde".to_string()), 'e');
65+
assert_eq!(
66+
Solution3::find_the_difference("abcd".to_string(), "abcde".to_string()),
67+
'e'
68+
);
69+
assert_eq!(
70+
Solution5::find_the_difference("abcd".to_string(), "abcde".to_string()),
71+
'e'
72+
);
1673
}
1774
}

0 commit comments

Comments
 (0)