+5
.zed/debug.json
+5
.zed/debug.json
+216
-74
README.md
+216
-74
README.md
···
13
13
14
14
### Blind 75
15
15
16
-
- [ ] Contains Duplicate
17
-
- [ ] Valid Anagram
18
-
- [ ] Two Sum
19
-
- [ ] Group Anagrams
20
-
- [ ] Top K Frequent Elements
21
-
- [ ] Encode and Decode Strings
22
-
- [ ] Product of Array Except Self
23
-
- [ ] Longest Consecutive Sequence
24
-
- [ ] Valid Palindrome
25
-
- [x] 3 Sum [[#15]](https://leetcode.com/problems/3sum/) src/two_pointers/three_sum.rs
26
-
- [x] Container With Most Water [[#11]](https://leetcode.com/problems/container-with-most-water/) src/two_pointers/max_area.rs
27
-
- [x] Best Time to Buy and Sell Stock [[#121]](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) src/sliding_window/max_profit.rs
16
+
#### Array & Hashing
17
+
18
+
- [ ] Contains Duplicate
19
+
[[#217]](https://leetcode.com/problems/contains-duplicate/)
20
+
- [x] Valid Anagram
21
+
[[#242]](https://leetcode.com/problems/valid-anagram/)
22
+
src/array_hashing/is_anagram.rs
23
+
- [x] Two Sum
24
+
[[#1]](https://leetcode.com/problems/two-sum/)
25
+
src/array_hashing/two_sum.rs
26
+
- [ ] Group Anagrams
27
+
[[#49]](https://leetcode.com/problems/group-anagrams/)
28
+
- [ ] Top K Frequent Elements
29
+
[[#347]](https://leetcode.com/problems/top-k-frequent-elements/)
30
+
- [ ] Encode and Decode Strings
31
+
[[#271]](https://leetcode.com/problems/encode-and-decode-strings/)
32
+
- [ ] Product of Array Except Self
33
+
[[#238]](https://leetcode.com/problems/product-of-array-except-self/)
34
+
- [ ] Longest Consecutive Sequence
35
+
[[#128]](https://leetcode.com/problems/longest-consecutive-sequence/)
36
+
37
+
#### Two Pointers
38
+
39
+
- [x] Valid Palindrome
40
+
[[#125]](https://leetcode.com/problems/valid-palindrome/)
41
+
src/two_pointers/is_palindrome.rs
42
+
- [x] 3 Sum
43
+
[[#15]](https://leetcode.com/problems/3sum/)
44
+
src/two_pointers/three_sum.rs
45
+
- [x] Container With Most Water
46
+
[[#11]](https://leetcode.com/problems/container-with-most-water/)
47
+
src/two_pointers/max_area.rs
48
+
49
+
#### Sliding Window
50
+
51
+
- [x] Best Time to Buy and Sell Stock
52
+
[[#121]](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
53
+
src/sliding_window/max_profit.rs
54
+
- [x] Longest Substring Without Repeating Characters
55
+
[[#3]](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
56
+
src/sliding_window/length_of_longest_substring.rs
57
+
- [x] Longest Repeating Character Replacement
58
+
[[#424]](https://leetcode.com/problems/longest-repeating-character-replacement/)
59
+
src/sliding_window/character_replacement.rs
60
+
- [x] Minimum Window Substring
61
+
[[#76]](https://leetcode.com/problems/minimum-window-substring/)
62
+
src/sliding_window/min_window.rs
63
+
64
+
65
+
#### Stack
66
+
67
+
- [x] Valid Parentheses
68
+
[[#20]](https://leetcode.com/problems/valid-parentheses/)
69
+
src/stack/is_valid_parentheses.rs
70
+
71
+
#### Binary Search
72
+
73
+
- [x] Find Minimum In Rotated Sorted Array
74
+
[[#153]](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
75
+
src/binary_search/find_min.rs
76
+
- [x] Search In Rotated Sorted Array
77
+
[[#33]](https://leetcode.com/problems/search-in-rotated-sorted-array/)
78
+
src/binary_search/search.rs
79
+
80
+
#### Linked List
28
81
29
-
- [ ] Longest Substring Without Repeating Characters
30
-
- [ ] Longest Repeating Character Replacement
31
-
- [ ] Minimum Window Substring
32
-
- [ ] Valid Parentheses
33
-
- [ ] Find Minimum In Rotated Sorted Array
34
-
- [ ] Search In Rotated Sorted Array
35
-
- [ ] Reverse Linked List
36
-
- [ ] Merge Two Sorted Lists
37
-
- [ ] Linked List Cycle
38
-
- [ ] Reorder List
39
-
- [ ] Remove Nth Node From End of List
40
-
- [ ] Merge K Sorted Lists
41
-
- [ ] Invert Binary Tree
42
-
- [ ] Maximum Depth of Binary Tree
43
-
- [ ] Same Tree
44
-
- [ ] Subtree of Another Tree
45
-
- [ ] Lowest Common Ancestor of a Binary Search Tree
46
-
- [ ] Binary Tree Level Order Traversal
47
-
- [ ] Validate Binary Search Tree
48
-
- [ ] Kth Smallest Element In a Bst
49
-
- [ ] Construct Binary Tree From Preorder And Inorder Traversal
50
-
- [ ] Binary Tree Maximum Path Sum
51
-
- [ ] Serialize And Deserialize Binary Tree
52
-
- [ ] Find Median From Data Stream
53
-
- [ ] Combination Sum
54
-
- [ ] Word Search
55
-
- [ ] Implement Trie Prefix Tree
56
-
- [ ] Design Add And Search Words Data Structure
57
-
- [ ] Word Search II
58
-
- [ ] Number of Islands
59
-
- [ ] Clone Graph
60
-
- [ ] Pacific Atlantic Water Flow
61
-
- [ ] Course Schedule
62
-
- [ ] Graph Valid Tree
63
-
- [ ] Number of Connected Components In An Undirected Graph
64
-
- [ ] Alien Dictionary
65
-
- [ ] Climbing Stairs
66
-
- [ ] House Robber
67
-
- [ ] House Robber II
68
-
- [ ] Longest Palindromic Substring
69
-
- [ ] Palindromic Substrings
70
-
- [ ] Decode Ways
71
-
- [ ] Coin Change
72
-
- [ ] Maximum Product Subarray
73
-
- [ ] Word Break
74
-
- [ ] Longest Increasing Subsequence
75
-
- [ ] Unique Paths
76
-
- [ ] Longest Common Subsequence
77
-
- [ ] Maximum Subarray
78
-
- [ ] Jump Game
79
-
- [ ] Insert Interval
80
-
- [ ] Merge Intervals
81
-
- [ ] Non Overlapping Intervals
82
-
- [ ] Meeting Rooms
83
-
- [ ] Meeting Rooms II
84
-
- [ ] Rotate Image
85
-
- [ ] Spiral Matrix
86
-
- [ ] Set Matrix Zeroes
87
-
- [ ] Number of 1 Bits
88
-
- [ ] Counting Bits
89
-
- [ ] Reverse Bits
90
-
- [ ] Missing Number
82
+
- [ ] Reverse Linked List
83
+
[[#206]](https://leetcode.com/problems/reverse-linked-list/)
84
+
src/linked_list/reverse_list.rs
85
+
- [ ] Merge Two Sorted Lists
86
+
[[#21]](https://leetcode.com/problems/merge-two-sorted-lists/)
87
+
src/linked_list/merge_two_lists.rs
88
+
- [ ] Linked List Cycle
89
+
[[#141]](https://leetcode.com/problems/linked-list-cycle/)
90
+
- [ ] Reorder List
91
+
[[#143]](https://leetcode.com/problems/reorder-list/)
92
+
- [ ] Remove Nth Node From End of List
93
+
[[#19]](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)
94
+
- [ ] Merge K Sorted Lists
95
+
[[#23]](https://leetcode.com/problems/merge-k-sorted-lists/)
96
+
97
+
#### Trees
98
+
99
+
- [ ] Invert Binary Tree
100
+
[[#226]](https://leetcode.com/problems/invert-binary-tree/)
101
+
- [ ] Maximum Depth of Binary Tree
102
+
[[#104]](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
103
+
- [ ] Same Tree
104
+
[[#100]](https://leetcode.com/problems/same-tree/)
105
+
- [ ] Subtree of Another Tree
106
+
[[#572]](https://leetcode.com/problems/subtree-of-another-tree/)
107
+
- [ ] Lowest Common Ancestor of a Binary Search Tree
108
+
[[#235]](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)
109
+
- [ ] Binary Tree Level Order Traversal
110
+
[[#102]](https://leetcode.com/problems/binary-tree-level-order-traversal/)
111
+
- [ ] Validate Binary Search Tree
112
+
[[#98]](https://leetcode.com/problems/validate-binary-search-tree/)
113
+
- [ ] Kth Smallest Element In a Bst
114
+
[[#230]](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)
115
+
- [ ] Construct Binary Tree From Preorder And Inorder Traversal
116
+
[[#105]](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
117
+
- [ ] Binary Tree Maximum Path Sum
118
+
[[#124]](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
119
+
- [ ] Serialize And Deserialize Binary Tree
120
+
[[#297]](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
121
+
122
+
#### Heap / Priority Queue
123
+
124
+
- [ ] Find Median From Data Stream
125
+
[[#295]](https://leetcode.com/problems/find-median-from-data-stream/)
126
+
127
+
#### Backtracking
128
+
129
+
- [ ] Combination Sum
130
+
[[#39]](https://leetcode.com/problems/combination-sum/)
131
+
- [ ] Word Search
132
+
[[#79]](https://leetcode.com/problems/word-search/)
133
+
134
+
#### Tries
135
+
136
+
- [ ] Implement Trie Prefix Tree
137
+
[[#208]](https://leetcode.com/problems/implement-trie-prefix-tree/)
138
+
- [ ] Design Add And Search Words Data Structure
139
+
[[#211]](https://leetcode.com/problems/design-add-and-search-words-data-structure/)
140
+
- [ ] Word Search II
141
+
[[#212]](https://leetcode.com/problems/word-search-ii/)
142
+
143
+
#### Graphs
144
+
145
+
- [ ] Number of Islands
146
+
[[#200]](https://leetcode.com/problems/number-of-islands/)
147
+
- [ ] Clone Graph
148
+
[[#133]](https://leetcode.com/problems/clone-graph/)
149
+
- [ ] Pacific Atlantic Water Flow
150
+
[[#417]](https://leetcode.com/problems/pacific-atlantic-water-flow/)
151
+
- [ ] Course Schedule
152
+
[[#207]](https://leetcode.com/problems/course-schedule/)
153
+
- [ ] Graph Valid Tree
154
+
[[#261]](https://leetcode.com/problems/graph-valid-tree/)
155
+
- [ ] Number of Connected Components In An Undirected Graph
156
+
[[#323]](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)
157
+
158
+
#### Advanced Graphs
159
+
160
+
- [ ] Alien Dictionary
161
+
[[#269]](https://leetcode.com/problems/alien-dictionary/)
162
+
163
+
#### 1-D Dynamic Programming
164
+
165
+
- [ ] Climbing Stairs
166
+
[[#70]](https://leetcode.com/problems/climbing-stairs/)
167
+
- [ ] House Robber
168
+
[[#198]](https://leetcode.com/problems/house-robber/)
169
+
- [ ] House Robber II
170
+
[[#213]](https://leetcode.com/problems/house-robber-ii/)
171
+
- [ ] Longest Palindromic Substring
172
+
[[#5]](https://leetcode.com/problems/longest-palindromic-substring/)
173
+
- [ ] Palindromic Substrings
174
+
[[#647]](https://leetcode.com/problems/palindromic-substrings/)
175
+
- [ ] Decode Ways
176
+
[[#91]](https://leetcode.com/problems/decode-ways/)
177
+
- [ ] Coin Change
178
+
[[#322]](https://leetcode.com/problems/coin-change/)
179
+
- [ ] Maximum Product Subarray
180
+
[[#152]](https://leetcode.com/problems/maximum-product-subarray/)
181
+
- [ ] Word Break
182
+
[[#139]](https://leetcode.com/problems/word-break/)
183
+
- [ ] Longest Increasing Subsequence
184
+
[[#300]](https://leetcode.com/problems/longest-increasing-subsequence/)
185
+
186
+
#### 2-D Dynamic Programming
187
+
188
+
- [ ] Unique Paths
189
+
[[#62]](https://leetcode.com/problems/unique-paths/)
190
+
- [ ] Longest Common Subsequence
191
+
[[#1143]](https://leetcode.com/problems/longest-common-subsequence/)
192
+
193
+
#### Greedy
194
+
195
+
- [ ] Maximum Subarray
196
+
[[#53]](https://leetcode.com/problems/maximum-subarray/)
197
+
- [ ] Jump Game
198
+
[[#55]](https://leetcode.com/problems/jump-game/)
199
+
200
+
#### Intervals
201
+
202
+
- [ ] Insert Interval
203
+
[[#57]](https://leetcode.com/problems/insert-interval/)
204
+
- [ ] Merge Intervals
205
+
[[#56]](https://leetcode.com/problems/merge-intervals/)
206
+
- [ ] Non Overlapping Intervals
207
+
[[#435]](https://leetcode.com/problems/non-overlapping-intervals/)
208
+
- [ ] Meeting Rooms
209
+
[[#252]](https://leetcode.com/problems/meeting-rooms/)
210
+
- [ ] Meeting Rooms II
211
+
[[#253]](https://leetcode.com/problems/meeting-rooms-ii/)
212
+
213
+
#### Math & Geometry
214
+
215
+
- [ ] Rotate Image
216
+
[[#48]](https://leetcode.com/problems/rotate-image/)
217
+
- [ ] Spiral Matrix
218
+
[[#54]](https://leetcode.com/problems/spiral-matrix/)
219
+
- [ ] Set Matrix Zeroes
220
+
[[#73]](https://leetcode.com/problems/set-matrix-zeroes/)
221
+
222
+
#### Bit Manipulation
223
+
224
+
- [ ] Number of 1 Bits
225
+
[[#191]](https://leetcode.com/problems/number-of-1-bits/)
226
+
- [ ] Counting Bits
227
+
[[#338]](https://leetcode.com/problems/counting-bits/)
228
+
- [ ] Reverse Bits
229
+
[[#190]](https://leetcode.com/problems/reverse-bits/)
230
+
- [ ] Missing Number
231
+
[[#268]](https://leetcode.com/problems/missing-number/)
91
232
- [ ] Sum of Two Integers
233
+
[[#371]](https://leetcode.com/problems/sum-of-two-integers/)
+34
src/array_hashing/is_anagram.rs
+34
src/array_hashing/is_anagram.rs
···
1
+
use std::collections::HashMap;
2
+
3
+
pub fn is_anagram(s: String, t: String) -> bool {
4
+
let mut hash = HashMap::new();
5
+
6
+
for char in s.chars() {
7
+
let entry = hash.entry(char).or_insert(0);
8
+
*entry += 1;
9
+
}
10
+
11
+
for char in t.chars() {
12
+
let entry = hash.entry(char).or_insert(0);
13
+
*entry -= 1;
14
+
}
15
+
16
+
for a in hash.into_values() {
17
+
if a != 0 {
18
+
return false;
19
+
}
20
+
}
21
+
22
+
true
23
+
}
24
+
25
+
#[cfg(test)]
26
+
mod tests {
27
+
use crate::array_hashing::is_anagram::is_anagram;
28
+
29
+
#[test]
30
+
fn case_1() {
31
+
assert_eq!(is_anagram("anagram".into(), "nagaram".into()), true);
32
+
assert_eq!(is_anagram("rat".into(), "car".into()), false);
33
+
}
34
+
}
+23
src/array_hashing/two_sum.rs
+23
src/array_hashing/two_sum.rs
···
1
+
use std::collections::HashMap;
2
+
3
+
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
4
+
let mut mp: HashMap<&i32, usize> = HashMap::new();
5
+
for (index, num) in numbers.iter().enumerate() {
6
+
if let Some(match_index) = mp.get(&(target - num)) {
7
+
return vec![*match_index as i32 + 1_i32, index.clone() as i32 + 1];
8
+
}
9
+
mp.insert(num, index);
10
+
}
11
+
return vec![];
12
+
}
13
+
14
+
#[cfg(test)]
15
+
mod tests {
16
+
use crate::array_hashing::two_sum::two_sum;
17
+
18
+
#[test]
19
+
fn case_1() {
20
+
assert_eq!(two_sum(vec![2, 7, 11, 15], 9,), vec![1, 2]);
21
+
assert_eq!(two_sum(vec![2, 3, 4], 6,), vec![1, 3]);
22
+
}
23
+
}
+37
src/binary_search/find_min.rs
+37
src/binary_search/find_min.rs
···
1
+
pub fn find_min(nums: Vec<i32>) -> i32 {
2
+
if nums.len() == 0 {
3
+
return -1;
4
+
}
5
+
let mut low = 0 as usize;
6
+
let mut high = nums.len() - 1;
7
+
let mut min = nums[low];
8
+
while high >= low {
9
+
if nums[low] < nums[high] {
10
+
min = nums[low].min(min);
11
+
break;
12
+
}
13
+
let mid = (high + low) / 2;
14
+
min = nums[mid].min(min);
15
+
if nums[mid] >= nums[low] {
16
+
low = mid + 1
17
+
} else {
18
+
high = mid - 1
19
+
}
20
+
}
21
+
min
22
+
}
23
+
24
+
#[cfg(test)]
25
+
mod tests {
26
+
use crate::binary_search::find_min::find_min;
27
+
28
+
#[test]
29
+
fn case_1() {
30
+
// rotated 3 times
31
+
assert_eq!(find_min(vec![3, 4, 5, 1, 2]), 1);
32
+
// rotated 4 times
33
+
assert_eq!(find_min(vec![4, 5, 6, 7, 0, 1, 2]), 0);
34
+
// rotated 4 times
35
+
assert_eq!(find_min(vec![11, 13, 15, 17]), 11);
36
+
}
37
+
}
+39
src/binary_search/search.rs
+39
src/binary_search/search.rs
···
1
+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
2
+
let mut low = 0;
3
+
let mut high = nums.len() - 1;
4
+
while low <= high {
5
+
let mid = (low + high) / 2;
6
+
if nums[mid] == target {
7
+
return mid as i32;
8
+
}
9
+
if nums[mid] >= nums[low] {
10
+
if target > nums[mid] || target < nums[low] {
11
+
low = mid + 1;
12
+
} else {
13
+
high = mid - 1;
14
+
}
15
+
} else {
16
+
if target < nums[mid] || target > nums[high] {
17
+
high = mid - 1;
18
+
} else {
19
+
low = mid + 1;
20
+
}
21
+
}
22
+
}
23
+
-1
24
+
}
25
+
26
+
#[cfg(test)]
27
+
mod tests {
28
+
use crate::binary_search::search::search;
29
+
30
+
#[test]
31
+
fn case_1() {
32
+
// rotated 3 times
33
+
assert_eq!(search(vec![4, 5, 6, 7, 0, 1, 2], 0), 4);
34
+
// rotated 4 times
35
+
assert_eq!(search(vec![4, 5, 6, 7, 0, 1, 2], 3), -1);
36
+
// rotated 4 times
37
+
assert_eq!(search(vec![1], 0), -1);
38
+
}
39
+
}
+3
src/lib.rs
+3
src/lib.rs
+35
src/sliding_window/character_replacement.rs
+35
src/sliding_window/character_replacement.rs
···
1
+
// Hash Table, String, Sliding Window
2
+
use std::collections::HashMap;
3
+
4
+
pub fn character_replacement(s: String, k: i32) -> i32 {
5
+
let mut count = HashMap::new();
6
+
let mut res = 0;
7
+
let mut left = 0;
8
+
let mut maxf = 0;
9
+
let chars: Vec<char> = s.chars().collect();
10
+
for right in 0..chars.len() {
11
+
let entry = count.entry(chars[right]).or_insert(0);
12
+
*entry += 1;
13
+
maxf = maxf.max(*entry);
14
+
while (right - left + 1) - maxf > k as usize {
15
+
let entry = count.entry(chars[left]).or_insert(0);
16
+
*entry -= 1;
17
+
left += 1;
18
+
}
19
+
res = res.max(right - left + 1);
20
+
}
21
+
22
+
return res as i32;
23
+
}
24
+
25
+
#[cfg(test)]
26
+
mod tests {
27
+
use crate::sliding_window::character_replacement::character_replacement;
28
+
29
+
#[test]
30
+
fn case_1() {
31
+
assert_eq!(character_replacement("XYYX".into(), 2), 4);
32
+
assert_eq!(character_replacement("AAABABB".into(), 1), 5);
33
+
assert_eq!(character_replacement("AABABBA".into(), 1), 4);
34
+
}
35
+
}
+24
src/sliding_window/length_of_longest_substring.rs
+24
src/sliding_window/length_of_longest_substring.rs
···
1
+
use std::collections::HashMap;
2
+
3
+
pub fn length_of_longest_substring(s: String) -> i32 {
4
+
let mut hash = HashMap::new();
5
+
let mut longest = 0;
6
+
let mut low = -1;
7
+
for (high, char) in s.chars().enumerate() {
8
+
if let Some(i) = hash.insert(char, high as i32) {
9
+
low = low.max(i);
10
+
}
11
+
longest = longest.max(high as i32 - low);
12
+
}
13
+
longest
14
+
}
15
+
16
+
#[cfg(test)]
17
+
mod tests {
18
+
use crate::sliding_window::length_of_longest_substring::length_of_longest_substring;
19
+
20
+
#[test]
21
+
fn case_1() {
22
+
assert_eq!(length_of_longest_substring("xz2qasze".into()), 6);
23
+
}
24
+
}
+107
src/sliding_window/min_window.rs
+107
src/sliding_window/min_window.rs
···
1
+
// use std::collections::HashMap;
2
+
3
+
fn hash(c: char) -> usize {
4
+
let c = c as u8;
5
+
if c >= b'a' {
6
+
(c - b'a') as usize
7
+
} else {
8
+
(c - b'A' + 26) as usize
9
+
}
10
+
}
11
+
12
+
pub fn min_window(s: String, t: String) -> String {
13
+
// first try
14
+
// if t.is_empty() {
15
+
// return String::new();
16
+
// }
17
+
// let mut counts = HashMap::new();
18
+
// let mut window = HashMap::new();
19
+
// let s_chars: Vec<char> = s.chars().collect();
20
+
// for c in t.chars() {
21
+
// *counts.entry(c).or_insert(0) += 1
22
+
// }
23
+
24
+
// let need = counts.len();
25
+
// let mut have = 0;
26
+
// let mut res = (-1_isize, -1_isize);
27
+
// let mut res_len = f32::MAX;
28
+
// let mut l = 0;
29
+
// for r in 0..s.len() {
30
+
// let right_c = s_chars[r];
31
+
// *window.entry(right_c).or_insert(0) += 1;
32
+
// if counts.contains_key(&right_c) && window[&right_c] == counts[&right_c] {
33
+
// have += 1;
34
+
// }
35
+
// while have == need {
36
+
// let len = (r - l) as f32 + 1_f32;
37
+
// if len < res_len {
38
+
// res = (l as isize, r as isize);
39
+
// res_len = len;
40
+
// }
41
+
// let left_c = s_chars[l];
42
+
// *window.entry(left_c).or_insert(0) -= 1;
43
+
// if counts.contains_key(&left_c) && window[&left_c] < counts[&left_c] {
44
+
// have -= 1
45
+
// }
46
+
// l += 1
47
+
// }
48
+
// }
49
+
// let (l, r) = res;
50
+
// if res_len != f32::MAX {
51
+
// s[l as usize..=r as usize].to_string()
52
+
// } else {
53
+
// String::new()
54
+
// }
55
+
56
+
// cheat
57
+
let s_vec: Vec<usize> = s.chars().map(|c| hash(c)).collect();
58
+
let mut counts = t
59
+
.chars()
60
+
.map(|c| hash(c))
61
+
.fold(vec![0i32; 52usize], |mut acc, c| {
62
+
acc[c] += 1;
63
+
acc
64
+
});
65
+
let mut unique = counts.iter().filter(|x| **x > 0).count();
66
+
67
+
let mut bounds = (0, 0);
68
+
let mut min_len = s.len() + 1;
69
+
let mut back = 0;
70
+
71
+
for (front, &c) in s_vec.iter().enumerate() {
72
+
counts[c] -= 1;
73
+
74
+
if counts[c] == 0 {
75
+
unique -= 1;
76
+
}
77
+
78
+
while unique == 0 {
79
+
if min_len > front - back {
80
+
bounds = (back, front + 1);
81
+
min_len = front - back;
82
+
}
83
+
84
+
counts[s_vec[back]] += 1;
85
+
86
+
if counts[s_vec[back]] > 0 {
87
+
unique += 1;
88
+
}
89
+
90
+
back += 1;
91
+
}
92
+
}
93
+
94
+
s[bounds.0..bounds.1].to_string()
95
+
}
96
+
97
+
#[cfg(test)]
98
+
mod tests {
99
+
use crate::sliding_window::min_window::min_window;
100
+
#[test]
101
+
fn case_1() {
102
+
assert_eq!(
103
+
min_window("OUZODYXAZV".into(), "XYZ".into()),
104
+
"YXAZ".to_string()
105
+
);
106
+
}
107
+
}
+3
src/sliding_window/mod.rs
+3
src/sliding_window/mod.rs
+49
src/stack/is_valid_parentheses.rs
+49
src/stack/is_valid_parentheses.rs
···
1
+
pub fn is_valid_parentheses(s: String) -> bool {
2
+
let mut stack = vec![];
3
+
for char in s.chars() {
4
+
match char {
5
+
'{' => {
6
+
stack.push('{');
7
+
}
8
+
'[' => {
9
+
stack.push('[');
10
+
}
11
+
'(' => {
12
+
stack.push('(');
13
+
}
14
+
')' => {
15
+
if let Some('(') = stack.pop() {
16
+
} else {
17
+
return false;
18
+
}
19
+
}
20
+
']' => {
21
+
if let Some('[') = stack.pop() {
22
+
} else {
23
+
return false;
24
+
}
25
+
}
26
+
'}' => {
27
+
if let Some('{') = stack.pop() {
28
+
} else {
29
+
return false;
30
+
}
31
+
}
32
+
_ => {}
33
+
}
34
+
}
35
+
stack.len() == 0
36
+
}
37
+
38
+
#[cfg(test)]
39
+
mod tests {
40
+
use crate::stack::is_valid_parentheses::is_valid_parentheses;
41
+
42
+
#[test]
43
+
fn case_1() {
44
+
assert_eq!(is_valid_parentheses("[]".into()), true);
45
+
assert_eq!(is_valid_parentheses("([{}])".into()), true);
46
+
assert_eq!(is_valid_parentheses("[(])".into()), false);
47
+
assert_eq!(is_valid_parentheses("])".into()), false);
48
+
}
49
+
}
+1
src/stack/mod.rs
+1
src/stack/mod.rs
···
1
+
pub mod is_valid_parentheses;
+33
src/two_pointers/is_palindrome.rs
+33
src/two_pointers/is_palindrome.rs
···
1
+
pub fn is_palindrome(s: String) -> bool {
2
+
let mut low = 0 as usize;
3
+
let mut high = s.len() - 1;
4
+
let chars: Vec<char> = s.chars().collect();
5
+
while high > low {
6
+
if !chars[high].is_alphanumeric() {
7
+
high -= 1;
8
+
continue;
9
+
}
10
+
if !chars[low].is_alphanumeric() {
11
+
low += 1;
12
+
continue;
13
+
}
14
+
15
+
if chars[high].to_ascii_lowercase() != chars[low].to_ascii_lowercase() {
16
+
return false;
17
+
}
18
+
high -= 1;
19
+
low += 1;
20
+
}
21
+
return true;
22
+
}
23
+
24
+
#[cfg(test)]
25
+
mod tests {
26
+
use crate::two_pointers::is_palindrome::is_palindrome;
27
+
28
+
#[test]
29
+
fn case_1() {
30
+
assert_eq!(is_palindrome("Was it a car or a cat I saw?".into()), true);
31
+
assert_eq!(is_palindrome("race a car".into()), false);
32
+
}
33
+
}