卖萌的弱渣

I am stupid, I am hungry.

Longest Substring Without Repeating Characters

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

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

Solution

  • Java
  • hash_map记录所有已经出现的字符最近的位置
  • j: 当前非重复串的起点, i: 当前非重复串的
(Longest-Substring-without-Repeating.java) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Solution {
        public int lengthOfLongestSubstring(String s) {
            if(s.length()==0) return 0;
            HashMap<Character, Integer> map = new HashMap<Character, Integer>();
            int ret = 0;
            // 不重复子字符串的起点
      int j=0;
            for(int i=0; i<s.length();i++){
               if(map.containsKey(s.charAt(i)))
                   j = Math.max(j, map.get(s.charAt(i))+1);
               map.put(s.charAt(i),i);
               ret = Math.max(ret, i-j+1);
            }
            return ret;
        }
}
  • Python
(Longest-Substring-Without-Repeating-Characters.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    n = len(string)
    cur_len = 1        # To store the lenght of current substring
    max_len = 1        # To store the result
    prev_index = 0    # To store the previous index
    i = 0

    # Initialize the visited array as -1, -1 is used to indicate
    # that character has not been visited yet.
    visited = [-1] * NO_OF_CHARS

    # Mark first character as visited by storing the index of
    # first character in visited array.
    visited[ord(string[0])] = 0

    # Start from the second character. First character is already
    # processed (cur_len and max_len are initialized as 1, and
    # visited[str[0]] is set
    for i in xrange(1,n):
        prev_index = visited[ord(string[i])]

        # If the currentt character is not present in the already
        # processed substring or it is not part of the current NRCS,
        # then do cur_len++
        if prev_index == -1 or (i - cur_len > prev_index):
            cur_len+=1

        # If the current character is present in currently considered
        # NRCS, then update NRCS to start from the next character of
        # previous instance.
        else:
            # Also, when we are changing the NRCS, we should also
            # check whether length of the previous NRCS was greater
            # than max_len or not.
            if cur_len > max_len:
                max_len = cur_len

            cur_len = i - prev_index

        # update the index of current character
        visited[ord(string[i])] = i

    # Compare the length of last NRCS with max_len and update
    # max_len if needed
    if cur_len > max_len:
        max_len = cur_len

    return max_len