Given a string s, find the length of the longest substring without repeating characters.
Question
Given a string s, find the length of the longest substring without repeating characters.
Solution
This problem can be solved using the sliding window technique in linear time complexity. Here are the steps:
-
Initialize two pointers,
startandend, to point to the first character of the string. Also, initialize a dictionary,char_dict, to keep track of the characters in the current substring and their indices. -
Move the
endpointer to the right one character at a time. For each character:a. If the character is not in
char_dict, add it tochar_dictwith its index as the value.b. If the character is already in
char_dict, it means we've found a repeating character. In this case, move thestartpointer to the right of the repeating character's previous index in the string. Also, update the character's index inchar_dict. -
After each move of the
endpointer, calculate the length of the current substring (i.e.,end - start + 1) and update the maximum length if necessary. -
Continue this process until the
endpointer reaches the end of the string. The maximum length found is the length of the longest substring without repeating characters.
Here is a Python code snippet that implements these steps:
def lengthOfLongestSubstring(s):
start = max_length = 0
char_dict = {}
for end in range(len(s)):
if s[end] in char_dict:
start = max(start, char_dict[s[end]] + 1)
char_dict[s[end]] = end
max_length = max(max_length, end - start + 1)
return max_length
This function returns the length of the longest substring without repeating characters in the input string s.
Similar Questions
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.
Longest Substring Without Repeating Characters
Write a function to find the longest palindromic substring in a given string
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.
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
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.