|
| 1 | +/** |
| 2 | + * [3] Longest Substring Without Repeating Characters |
| 3 | + * |
| 4 | + * Given a string, find the length of the longest substring without repeating characters. |
| 5 | + * |
| 6 | + * <div> |
| 7 | + * Example 1: |
| 8 | + * |
| 9 | + * |
| 10 | + * Input: <span id="example-input-1-1">"abcabcbb"</span> |
| 11 | + * Output: <span id="example-output-1">3 |
| 12 | + * Explanation:</span> The answer is "abc", with the length of 3. |
| 13 | + * |
| 14 | + * |
| 15 | + * <div> |
| 16 | + * Example 2: |
| 17 | + * |
| 18 | + * |
| 19 | + * Input: <span id="example-input-2-1">"bbbbb"</span> |
| 20 | + * Output: <span id="example-output-2">1 |
| 21 | + * </span><span id="example-output-1">Explanation: T</span>he answer is "b", with the length of 1. |
| 22 | + * |
| 23 | + * |
| 24 | + * <div> |
| 25 | + * Example 3: |
| 26 | + * |
| 27 | + * |
| 28 | + * Input: <span id="example-input-3-1">"pwwkew"</span> |
| 29 | + * Output: <span id="example-output-3">3 |
| 30 | + * </span><span id="example-output-1">Explanation: </span>The answer is "wke", with the length of 3. |
| 31 | + * Note that the answer must be a substring, "pwke" is a subsequence and not a substring. |
| 32 | + * |
| 33 | + * </div> |
| 34 | + * </div> |
| 35 | + * </div> |
| 36 | + * |
| 37 | + */ |
| 38 | +pub struct Solution {} |
| 39 | + |
| 40 | +// problem: https://leetcode.com/problems/longest-substring-without-repeating-characters/ |
| 41 | +// discuss: https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/?currentPage=1&orderBy=most_votes&query= |
| 42 | + |
| 43 | +// submission codes start here |
| 44 | + |
| 45 | +use std::collections::{HashSet, HashMap}; |
| 46 | +impl Solution { |
| 47 | + pub fn length_of_longest_substring(s: String) -> i32 { |
| 48 | + let mut start = 0; |
| 49 | + let mut max_substr_len = 0; |
| 50 | + let mut occurrence_map = HashSet::with_capacity(s.len()); |
| 51 | + let mut chr_idx_map = HashMap::with_capacity(s.len()); |
| 52 | + |
| 53 | + // Don't consider Unicode Strings (related with graphemes) |
| 54 | + |
| 55 | + for (idx, chr) in s.chars().enumerate() { |
| 56 | + // Make substring that ends with (idx, chr) |
| 57 | + |
| 58 | + // (0) String preprocessing to make further O(1) string indexing |
| 59 | + chr_idx_map.insert(idx, chr); |
| 60 | + |
| 61 | + // (1) If s[idx] exists in substring s[start:idx-1], keep removing first char of s[start:idx-1] until there is no s[idx] |
| 62 | + while occurrence_map.contains(&chr) { |
| 63 | + assert!(start < idx); |
| 64 | + |
| 65 | + let fst_chr = chr_idx_map.get(&start).unwrap(); |
| 66 | + occurrence_map.remove(fst_chr); |
| 67 | + start += 1; |
| 68 | + } |
| 69 | + |
| 70 | + // (2) Now substring s[start:idx] doesn't contain repeating character. Update occurrence_map. |
| 71 | + occurrence_map.insert(chr); |
| 72 | + |
| 73 | + // (3) update max_len |
| 74 | + let new_substr_len = idx - start + 1; |
| 75 | + max_substr_len = if new_substr_len >= max_substr_len { new_substr_len } else { max_substr_len }; |
| 76 | + } |
| 77 | + |
| 78 | + max_substr_len as i32 |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// submission codes end |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_3() { |
| 90 | + assert_eq!(Solution::length_of_longest_substring(String::from("abcabcbb")), 3); |
| 91 | + assert_eq!(Solution::length_of_longest_substring(String::from("bbbbb")), 1); |
| 92 | + assert_eq!(Solution::length_of_longest_substring(String::from("pwwkew")), 3); |
| 93 | + assert_eq!(Solution::length_of_longest_substring(String::from("a")), 1); |
| 94 | + } |
| 95 | +} |
0 commit comments