Given a string s, find the length of the longest substring without repeating characters. Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints:0 <= s.length <= 5 * 104s consists of English letters, digits, symbols and spaces.
Question
Given a string s, find the length of the longest substring without repeating characters. Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints:0 <= s.length <= 5 * 104s consists of English letters, digits, symbols and spaces.
Solution
Sure, here is a step-by-step solution in Python:
- Initialize two pointers, start and end, to 0. These pointers represent the current substring we are looking at.
- Initialize a dictionary to keep track of the characters in the current substring and their indices.
- While the end pointer is less than the length of the string:
- If the character at the end pointer is not in the dictionary, add it to the dictionary and increment the end pointer.
- If the character at the end pointer is in the dictionary, remove the character at the start pointer from the dictionary and increment the start pointer.
- At each step, calculate the length of the current substring (end - start) and keep track of the maximum length.
- Return the maximum length.
Here is the Python code for the above steps:
def lengthOfLongestSubstring(s):
start = end = max_length = 0
char_dict = {}
while end < len(s):
if s[end] not in char_dict:
char_dict[s[end]] = 1
max_length = max(max_length, end - start + 1)
end += 1
else:
del char_dict[s[start]]
start += 1
return max_length
This function works by maintaining a sliding window of unique characters and adjusting its size as necessary by moving the start and end pointers. The maximum length of the window at any point is the length of the longest substring without repeating characters.
Similar Questions
Given a string s, find the length of the longest substring without repeating characters.
Longest Substring Without Repeating Characters
Given a string s consisting of words and spaces, return the length of the last word in the string.A word is a maximal substring consisting of non-space characters only. Example 1:Input: s = "Hello World"Output: 5Explanation: The last word is "World" with length 5.Example 2:Input: s = " fly me to the moon "Output: 4Explanation: The last word is "moon" with length 4.Example 3:Input: s = "luffy is still joyboy"Output: 6Explanation: The last word is "joyboy" with length 6. Constraints:1 <= s.length <= 104s consists of only English letters and spaces ' '.There will be at least one wor
Raju is preparing questions for the internal test. While creating questions, he gave one such question to his son Arun to solve. Raju has given the string S to his son Arun. Arun's task is to print the size of the longest possible substring that has exactly K unique characters.ExampleInput:String = aabacbebebe; K = 3Output:The substring will be cbebebe with length 7. Explanation:The unique characters in the string are: 'a', 'b', 'c', and 'e'. The longest subsequence with at most 3 distinct characters is "aabacbe". It contains the characters 'a', 'b', and 'c', which is the maximum number of distinct characters allowed (k = 3). Therefore, the length of the longest subsequence is 7.Input format :The first line of input consists of the string S.The second line of input consists of the integer K.Output format :The output prints the size of the longest possible substring that has exactly K unique characters.Refer to the sample output for the formatting specifications.Code constraints :1≤ string length ≤ 501≤ K ≤10Sample test cases :Input 1 :aabacbebebe3Output 1 :7Input 2 :abbc1Output 2 :2
Given a string s, return the longest palindromic substring in s. Example 1:Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.