Knowee
Questions
Features
Study Tools

Longest Substring Without Repeating CharactersGiven a string s find the length of the longest  substring without repeating characters.

Question

Longest Substring Without Repeating CharactersGiven a string s find the length of the longest  substring without repeating characters.

🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using the sliding window technique. Here are the steps in Python:

  1. Initialize two pointers, start and end, to point to the beginning of the string. These pointers will define our current substring.

  2. Initialize a dictionary, used_chars, to keep track of the characters we have seen and their latest positions.

  3. While end is less than the length of the string, do the following:

    • If the character at end is not in used_chars, add it to used_chars and increment end.

    • If the character at end is in used_chars, move start to the right of the previous instance of the character at end and update its position in used_chars.

  4. The length of the longest substring without repeating characters is the maximum length of the substring we have seen during this process.

Here is the Python code for the above steps:

def lengthOfLongestSubstring(s):
    start = max_length = 0
    used_chars = {}

    for i, char in enumerate(s):
        if char in used_chars and start <= used_chars[char]:
            start = used_chars[char] + 1
        else:
            max_length = max(max_length, i - start + 1)

        used_chars[char] = i

    return max_length

This function works by keeping track of the longest substring seen so far and the start of the current substring. When it sees a repeating character, it moves the start of the current substring to the right of the previous instance of the repeating character and updates the position of the repeating character. The length of the longest substring is updated whenever a longer substring is found.

This problem has been solved

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.

Write a function to find the longest palindromic substring in a given string

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.

Write a Java program to find longest Palindromic Substring within a string CopySample Output:The given string is: thequickbrownfoxxofnworbquicktheThe longest palindrome substring in the given string is; brownfoxxofnworbThe length of the palindromic substring is: 16

1/3

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.