|
| 1 | +--- |
| 2 | +id: longest-substrings-without-repeating-characters |
| 3 | +title: Longest Substring Without Repeating Characters (LeetCode) |
| 4 | +sidebar_label: 0003 - Longest Substring Without Repeating Characters |
| 5 | +tags: |
| 6 | + - Hash table |
| 7 | + - String |
| 8 | + - Sliding Window |
| 9 | +description: "This is a solution to the Longest Substring Without Repeating Characters problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 15 | +| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------- | |
| 16 | +| [Longest Substring Without Repeating Characters on LeetCode](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Longest Substring Without Repeating Characters Solution on LeetCode](https://leetcode.com/problems/longest-substring-without-repeating-characters/solutions/5234730/solution/) | [Amruta Jayanti](https://leetcode.com/u/user7669cY/)| |
| 17 | + |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +Given a string `s`, find the length of the **longest substring** without repeating characters. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +**Example 1:** |
| 26 | + |
| 27 | +```plaintext |
| 28 | +Input: s = "abcabcbb" |
| 29 | +Output: 3 |
| 30 | +Explanation: The answer is "abc", with the length of 3. |
| 31 | +``` |
| 32 | + |
| 33 | +**Example 2:** |
| 34 | + |
| 35 | +```plaintext |
| 36 | +Input: s = "bbbbb" |
| 37 | +Output: 1 |
| 38 | +Explanation: The answer is "b", with the length of 1. |
| 39 | +``` |
| 40 | + |
| 41 | +**Example 3:** |
| 42 | + |
| 43 | +```plaintext |
| 44 | +Input: s = "pwwkew" |
| 45 | +Output: 3 |
| 46 | +Explanation: The answer is "wke", with the length of 3. |
| 47 | +Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. |
| 48 | +``` |
| 49 | + |
| 50 | +### Constraints: |
| 51 | + |
| 52 | + |
| 53 | +- `0 <= s.length <= 5 * 104` |
| 54 | +- `s` consists of English letters, digits, symbols and spaces. |
| 55 | + |
| 56 | + |
| 57 | +## Solution to the problem |
| 58 | + |
| 59 | +### Intuition and Approach |
| 60 | +This is a simple problem.Here try to maintain the unique characters and erase the repeating character. |
| 61 | +#### Implementation |
| 62 | +```python |
| 63 | +class Solution: |
| 64 | + def lengthOfLongestSubstring(self, s: str) -> int: |
| 65 | + n = len(s) |
| 66 | + maxLength = 0 |
| 67 | + charSet = set() |
| 68 | + left = 0 |
| 69 | + |
| 70 | + for right in range(n): |
| 71 | + if s[right] not in charSet: |
| 72 | + charSet.add(s[right]) |
| 73 | + maxLength = max(maxLength, right - left + 1) |
| 74 | + else: |
| 75 | + while s[right] in charSet: |
| 76 | + charSet.remove(s[left]) |
| 77 | + left += 1 |
| 78 | + charSet.add(s[right]) |
| 79 | + |
| 80 | + return maxLength |
| 81 | +``` |
| 82 | +Above is the implementation in Python. |
| 83 | + |
| 84 | +#### Complexity Analysis: |
| 85 | +- Time Complexity : $$O(n)$$ Here, n is the length of the substring |
| 86 | +- Space Complexity : $$O(min(n,m))$$ Here, m is the size of character set |
| 87 | + |
| 88 | +#### Codes in different languages: |
| 89 | +`CPP`: |
| 90 | +```cpp |
| 91 | +#include <vector> |
| 92 | +#include <string> |
| 93 | +#include <algorithm> |
| 94 | + |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + int lengthOfLongestSubstring(string s) { |
| 98 | + if (s.empty()) return 0; |
| 99 | + |
| 100 | + int maxLength = 0; |
| 101 | + std::vector<char> seen; |
| 102 | + |
| 103 | + for (char c : s) { |
| 104 | + auto it = std::find(seen.begin(), seen.end(), c); |
| 105 | + if (it == seen.end()) { |
| 106 | + seen.push_back(c); |
| 107 | + maxLength = std::max(maxLength, static_cast<int>(seen.size())); |
| 108 | + } else { |
| 109 | + seen.erase(seen.begin(), it + 1); // Remove characters before the repeated character |
| 110 | + seen.push_back(c); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return maxLength; |
| 115 | + } |
| 116 | +}; |
| 117 | +``` |
| 118 | + |
| 119 | +`Java`: |
| 120 | +```java |
| 121 | +class Solution { |
| 122 | + public int lengthOfLongestSubstring(String s) { |
| 123 | + int n = s.length(); |
| 124 | + int maxLength = 0; |
| 125 | + Set<Character> charSet = new HashSet<>(); |
| 126 | + int left = 0; |
| 127 | + |
| 128 | + for (int right = 0; right < n; right++) { |
| 129 | + if (!charSet.contains(s.charAt(right))) { |
| 130 | + charSet.add(s.charAt(right)); |
| 131 | + maxLength = Math.max(maxLength, right - left + 1); |
| 132 | + } else { |
| 133 | + while (charSet.contains(s.charAt(right))) { |
| 134 | + charSet.remove(s.charAt(left)); |
| 135 | + left++; |
| 136 | + } |
| 137 | + charSet.add(s.charAt(right)); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return maxLength; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
0 commit comments