BackmediumSliding Window Flipkart

Unique Characters Substring Solution

Problem Statement

Given a string s, find the length of the longest continuous segment of packet types with no repetition.

Example 1
Input
abcabc
Output
2

Explanation: Step-by-step: with input 'abcabc', we initialize the sliding window of size 3 as 'abc'. The set of unique characters in this window is {'a', 'b', 'c'}. Since the window size is equal to the set size, we update maxLen to 3. Then, we slide the window to the right by one character. The new window is 'bcab'. The set of unique characters in this window is {'b', 'c', 'a'}. Again, the window size is equal to the set size, so we update maxLen to 3. However, we notice that the window 'bcab' does not contain unique characters because 'a' is repeated. Therefore, we reset maxLen to 2.

Example 2
Input
aabaa
Output
2

Explanation: Step-by-step: with input 'aabaa', we initialize the sliding window of size 3 as 'aab'. The set of unique characters in this window is {'a', 'b'}. Since the window size is equal to the set size, we update maxLen to 3. Then, we slide the window to the right by one character. The new window is 'aba'. The set of unique characters in this window is {'a', 'b', 'a'}. However, we notice that the window 'aba' does not contain unique characters because 'a' is repeated. Therefore, we reset maxLen to 2.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, symbols and spaces.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free