Last active
May 31, 2023 19:09
-
-
Save Josephchinedu/b8839adc4ce3d6b1c944abb0adc845d8 to your computer and use it in GitHub Desktop.
Longest Substring Without Repeating Characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Question: | |
Given a string s, find the length of the longest substring without repeating characters. | |
Solution: | |
using sliding window technique and hash map. | |
Window Sliding Technique is a computational technique that aims to reduce the use of nested loops and replace it with a single loop, thereby reducing the time complexity. | |
steps: | |
1. Initialize two pointers, 'start' and 'end,' both pointing to the start of the string. | |
2. Initialize an empty hash set to store the unique characters. | |
3. Initialize a variable 'maxLen' to store the maximum length encountered so far. | |
4. Iterate through the string with the 'end' pointer: | |
a. Check if the current character at 'end' is already in the hash set. | |
. If it is, remove the character at 'start' from the hash set and increment 'start' by 1. | |
. If it is not, add the current character to the hash set, calculate the length of the current substring (end - start + 1), and update 'maxLen' if necessary. | |
5. Repeat step 4 until the 'end' pointer reaches the end of the string. | |
6. Return 'maxLen' as the result. | |
Time complexity: O(N), Where N is the length of the input string. | |
Space complexity: O(1). | |
https://leetcode.com/problems/longest-substring-without-repeating-characters/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment