Daily brushing title 191122

  A slag slag Bloggers, brush leetcode Chou Chou himself, the great God who made better way to look to the wing. Title and solution from power button (LeetCode), Portal .

algorithm:

Given a string, you find out which does not contain a sub-length of the longest string of repeated characters.

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: Because the longest substring of characters without repetition is "abc", so its length is 3.
Example 2:

Input: "bbbbb"
Output: 1
Explanation: Because the longest substring is repeated characters without "b", so that its length is 1.
Example 3:

Input: "pwwkew"
Output: 3
Explanation: Because the longest sub-string is repeated characters without "wke", so its length is 3.
  Please note that your answer must be a substring of length, "pwke" is a sub-sequence, not a substring.

public class Solution {
        public int LengthOfLongestSubstring(string s)
        {
            var includeList = new List<char>();

            int maxCount = 0;
            int count = 0;

            var strArray = s.ToArray();

            for (int i = 0; i < strArray.Length; i++)
            {
                if (!includeList.Contains(strArray[i]))
                {
                    count++;
                    includeList.Add(strArray[i]);

                    if (count > maxCount)
                    {
                        maxCount = count;
                    }
                }
                else
                {
                    var duplicateIndex = includeList.IndexOf(strArray[i]);
                    count = includeList.Count - duplicateIndex;

                    var tempIncludeList = includeList.Skip(duplicateIndex + 1).ToList();
                    includeList = tempIncludeList;
                    includeList.Add(strArray[i]);
                }
            }

            return maxCount;
        }
}

  This question idea is that, with a List to store the current does not overlap the entire string. Focus is experiencing a repeat of the first string of the time. This time, a forward starting from this position, if there is longer than the maximum length of the string before the updates, or to maintain the status quo. This question of boundary value very interesting. We can look at these test use cases: abcabc, bbbb, dvdf, pwwekw, anviaj and so on. Corresponding to different situations, different locations, such as repeating characters in the string before the maximum, as pwwekw, w pw longest end string before, anviaj first maximum string before.

  Therefore, when we find repeated characters, first find from the current position forward repeating characters, the length between the foundation of our next comparison is the longest length. At this time, the count thus calculated is actually includeList.Count - (duplicateIndex + 1) + 1. I.e., the number of characters of two direct repeats this character + the character.

  But the efficiency of this method and time complexity of the above are not enough to spend, there is room for optimization.

  Official Portal Solution: https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-de-zui-chang-zi-chuan- by-leetcod /

  The second method, the sliding window is very interesting, efficiency is good enough!

Guess you like

Origin www.cnblogs.com/dogtwo0214/p/11913244.html