LeetCode 3: Longest Substring Without Repeating Characters

Posted by pandaweb on Mon, 16 Dec 2019 08:13:29 +0100

Title:

Given a string, please find out the length of the longest substring that does not contain duplicate characters.

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Type: "abcabcbb"
Output: 3 
Explanation: because the longest substring without repeating characters is "abc", its length is 3.

Example 2:

Enter: "bbbbb"
Output: 1
 Explanation: because the longest substring without repeating characters is "b", its length is 1.

Example 3:

Enter: "pwwkew"
Output: 3
 Explanation: because the longest substring without repeating characters is "wke", its length is 3.
     Note that your answer must be the length of the substring, "pwke" is a substring, not a substring.

Solutions:

  • Brute force solution, time complexity is O(n^3), because to traverse all characters, traverse the substring to confirm whether there are duplicate characters, pass

  • Sliding window, maintain a sliding window of index [i, J], update the sliding window [i ', J] directly for the existing characters i', you need to keep each character value and its index, that is, the index position is mapped by the characters

    • Hash map: Key is character Value, Value is index position
    • Character mapping: there are 128 characters in ASCII code, maintaining an integer array with length of 128, mapping 128 characters in array index value, and storing element value in character position

Hash map:

Java:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();//Convert to character array
        if (chars.length == 0) return 0;
        HashMap<Character, Integer> map = new HashMap<>();//Build hash map
        int size = s.length(), count = 0, i = 0;
        for (int j = 0; j < size; j++) {//Ergodic character
            if (map.containsKey(chars[j]))//If the character exists in the map
                i = Math.max(map.get(chars[j]), i);//Update left boundary i of sliding window
            count = Math.max(count, j - i);//Update count to maximum
            map.put(chars[j], j + 1);//Update the Value of the character mapping in the mapping to add one to the current position
        }
        return count + 1;//Return the maximum cumulative total, and add 1
    }
}

Python:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if not s:
            return 0
        hash_map = dict() # Build a dictionary
        size = len(s)
        i, count = 0, 0
        for j, c in enumerate(s): # Enumeration character
            if c in hash_map: # If the character exists in the map
                i = max(i, hash_map[c]) # Update left boundary i of sliding window
            count = max(count, j-i) # Update count to maximum
            hash_map[c] = j+1 # Update the Value of the character mapping in the mapping to add one to the current position
        return count+1 # Return the maximum cumulative total, and add 1

Character mapping:

Java:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        if (chars.length == 0) return 0;
        int[] index = new int[128];//Establish a 128 bit ASCII character mapping table
        int size = s.length(), count = 0, i = 0;
        for (int j = 0; j < size; j++) {//Ergodic character
            i = Math.max(index[chars[j]], i);//Update left boundary i of sliding window
            count = Math.max(count, j - i);//Update count to maximum
            index[chars[j]] = j + 1;//The element value of the character in the update map is the current position plus one
        }
        return count + 1;//Return the maximum cumulative total, and add 1
    }
}

Python:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if not s:
            return 0
        size = len(s)
        i, count = 0, 0
        index = [0]*128 # Establish a 128 bit ASCII character mapping table
        for j, c in enumerate(s): # Enumeration character
            i = max(i, index[ord(c)]) # Update left boundary i of sliding window
            count = max(count, j-i) # Update count to maximum
            index[ord(c)] = j+1 # The element value of the character in the update map is the current position plus one
        return count+1 # Return the maximum cumulative total, and add 1

Because Python has no character concept, the ord() function needs to be converted to ASCII number

Welcome to wechat. Xingong. Zhonghao: love to write Bug

Topics: ascii Python Java