Skip to content

Commit da34d25

Browse files
committed
docs: add algorithm description for minimum window substring
1 parent ce159ff commit da34d25

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/hard/minimum_window_substring.rs

+20
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ pub fn min_window(s: String, t: String) -> String {
4040
min_substring
4141
}
4242

43+
/*
44+
Algorithm - Sliding Window
45+
- Create a need_map to store the number of characters in t
46+
- Create a window_map to store the number of characters in the window
47+
- Create a count to store the number of characters in the window that matches the characters in t
48+
- Create a left and right pointer to shrink and expand the window
49+
- Create a min_substring to store the minimum substring that contains all the characters in t
50+
- Loop through the string
51+
- If the window contains all the characters in t
52+
- Try to shrink the window
53+
- If the window is smaller than the min_substring
54+
- Update the min_substring
55+
- If the window does not contain all the characters in t
56+
- Try to expand the window
57+
- Return the min_substring
58+
59+
Time: O(n)
60+
Space: O(n)
61+
*/
62+
4363
#[cfg(test)]
4464
mod tests {
4565
use super::*;

0 commit comments

Comments
 (0)