[Leetcode] Longest substring without repeating characters

Source: Internet
Author: User

Given a string, find the length of the longest substring without repeating characters. for example, the longest substring without repeating letters for "abcabcbb" is "ABC", which the length is 3. for "bbbbb" the longest substring is "B", with the length of 1.

Https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/

 

Idea: use an exist array to save whether or not the characters appear (assuming that char set is ASCII) and traverse the array from front to back. If an existing character exists, it should be rolled back to the next position where the character appeared last time to start statistics (for example), and pay attention to the simultaneous update of the exist array.

 

public class Solution {    public int lengthOfLongestSubstring(String s) {        if (s == null || s.length() == 0)            return 0;        int res = 1;        boolean[] exist = new boolean[256];        int start = 0;        for (int i = 0; i < s.length(); i++) {            char ch = s.charAt(i);            if (exist[ch]) {                res = Math.max(res, i - start);                for (int k = start; k < i; k++) {                    if (s.charAt(k) == ch) {                        start = k + 1;                        break;                    }                    exist[s.charAt(k)] = false;                }            } else                exist[ch] = true;        }        res = Math.max(res, s.length() - start);        return res;    }    public static void main(String[] args) {        System.out.println(new Solution().lengthOfLongestSubstring("abcabcbb"));        System.out.println(new Solution().lengthOfLongestSubstring("bbbbb"));        System.out.println(new Solution()                .lengthOfLongestSubstring("fpdcztbudxfipowpnamsrfgexjlbjrfoglthewbhtiriznzmolehqnlpwxrfowwwjrd"));    }}

 

 

 

Refer:

Http://blog.csdn.net/likecool21/article/details/10858799

Http://www.programcreek.com/2013/02/leetcode-longest-substring-without-repeating-characters-java/

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.