[Leetcode] The eldest son string with no repeating character _leetcode

Source: Internet
Author: User
The eldest son with no repeat character in English description

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

Examples:
Given "ABCABCBB", the answer is "ABC" and 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 to be a substring, "Pwke" are a subsequence and not a substring. Chinese description

Given a string, find the length of the oldest string that does not contain duplicate characters.

Example:

Given "ABCABCBB", the oldest string without repeated characters is "abc", then the length is 3.
Given "BBBBB", the longest substring is "B", the length is 1.
Given "Pwwkew", the eldest son string is "Wke", and the length is 3. Note that the answer must be a substring, and "Pwke" is a subsequence rather than a substring.

Algorithm reference: https://www.nowcoder.com/questionTerminal/5947ddcc17cb4f09909efa7342780048

"Sliding window Solution"
For example ABCABCCC when you scan the right to the ABCA you have to delete the first A to get BCA,
and then the "window" continues to slide to the right, and whenever a new char is added, the left side checks for duplicate char,
Then if there is no repetition of the normal add,
there are repeated words on the left to throw away part (from the leftmost to repeat char this paragraph thrown away), in this process to record the maximum window length

By code:

 int lengthoflongestsubstring (string inputstring) {Unordered_map<char, int> charmap = {};
        int maxLength = 0;
        int leftpos = 0;

            for (Auto i = 0; i < inputstring.size (); i++) {char Curchar = inputstring[i];//current character if (Charmap.find (Curchar)!= charmap.end ()) {//The current character has already appeared, find its position int Existchar
                Pos = Charmap[curchar];
            LeftPos = Max (LeftPos, Existcharpos + 1);
            } maxLength = max (maxLength, I-leftpos + 1);
        Charmap[curchar] = i;
    return maxLength; }

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.