3. Longest Substring Without Repeating Characters

Description
Total Accepted: 283937
Total Submissions: 1178327
Difficulty: Medium
Contributor: LeetCode
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.

Hide Company Tags Amazon Adobe Bloomberg Yelp
Hide Tags Hash Table Two Pointers String
Hide Similar Problems (H) Longest Substring with At Most Two Distinct Characters


package string;

public class LengthOfLongestSubstring {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
      String[] tests = {"ctnlk", "z", "aaaa", "aab", "", "an++--viaj"};
      int[] res = {5, 1, 1, 2, 0, 5};
      for (int i = 0; i < tests.length; i++) {
          int ans = lengthOfLongestSubstring(tests[i]); 
          System.out.println(ans == res[i]);
      }
    }
    
    public static int lengthOfLongestSubstring(String s) {
      int[] map = new int[256]; // map from character's ASCII to its last occured index
      int j = 0;
      int len = 0;
      for (int i = 0; i < s.length(); i++) {
          while (j < s.length() && map[s.charAt(j)] == 0) {
              map[s.charAt(j)] = 1;                   
              len = Math.max(len,  j - i + 1);
              j++;
          }
          map[s.charAt(i)] = 0;
      }
      return len;
    }
    
    
    public static int lengthOfLongestSubstring_failed(String s) {
        if (s == null || s.length() == 0 || s.length() == 1) return s.length();
        int len = 0;
        char[] arr = s.toCharArray();
        
        int j = 0;
        for (int i = 0; i < s.length(); i++) {
                while (j < s.length()) {
                  if(j < s.length() && arr[i] != arr[j]) {
                    // System.out.printf("arr[%d] = %c \t", j, arr[j]);
                    len = Math.max(len, j - i + 1);
                    j++;
                  } 
                  if (j < s.length() && arr[i] == arr[j]) {
                    len = Math.max(len, j - i );
                    i = j;
                    j++;
                  }              
                }
            }
        
        return len;
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容