leetcode1750: The shortest length after deleting the same characters at both ends of the string (12.28 daily question)

Topic statement:

Given a string containing only the characters   ,  'a'and 'b' ,   you can do the following (5 steps) any number of times:'c's

  1. Selects  s a  non-empty  prefix of strings where all characters of the prefix are identical.
  2. Selects a string  with s a  non-empty  suffix where all characters are the same.
  3. Prefixes and suffixes cannot intersect anywhere in the string.
  4. Prefix and suffix must contain all the same characters.
  5. Both prefix and suffix are removed.

Please return  the shortest lengths  that can be obtained after performing the above operations on the string any number of times (possibly 0 times)   .

Example 1:

Input: s = "ca"
 Output: 2
 Explanation: You can't delete any characters, so the string length remains the same.

Example 2:

Input: s = "cabaabac"
 Output: 0
 Explanation: The optimal sequence of operations is:
- Select the prefix "c" and the suffix "c" and delete them, resulting in s = "abaaba".
- Select the prefix "a" and the suffix "a" and delete them, resulting in s = "baab".
- Select prefix "b" and suffix "b" and delete them, resulting in s = "aa".
- Select the prefix "a" and the suffix "a" and delete them, resulting in s = "".

Example 3:

Input: s = "aabccabba"
 Output: 3
 Explanation: The optimal sequence of operations is:
- Select the prefix "aa" and the suffix "a" and delete them, resulting in s = "bccabb".
- Select the prefix "b" and the suffix "bb" and delete them, resulting in s = "cca".

hint:

  • 1 <= s.length <= 100000
  • s Contains only characters  'a', 'b' and  'c' .

Problem-solving ideas:

        The while loop condition is that the last character of the string s is the same as the first character.

        The conditional statement in while is to find and delete the characters with the same prefix and the same suffix.

        If the length of the remaining s is equal to 1, return 1 directly;

        If the first and last characters of the string are different, return the length of the string s.

Problem-solving code (99/100, the last case failed):

class Solution {
public:
    int minimumLength(string s) {  //最后一个用例通过不了
        while(s[0]==s[s.length()-1])
        {
            if(s.length()==1)
            return 1;
            int start=0;
            int end=s.length()-1;
            while(start<s.length()&&s[start]==s[0])
            {
                start++;
            }
            while(end>=0&&s[end]==s[s.length()-1])
            {
                end--;
            }
            if(end<start)
            {
                return 0;
            }
            else
            {
                s=s.substr(start);
                s=s.substr(0,end-start+1);
            }
        }
        return s.length();
    }
};

Problem-solving code:

class Solution {   //官方题解
public:
    int minimumLength(string s) {
        int n = s.size();
        int left = 0, right = n - 1;
        while (left < right && s[left] == s[right]) {
            char c = s[left];
            while (left <= right && s[left] == c) {
                left++;
            }
            while (left <= right && s[right] == c) {
                right--;
            }
        }
        return right - left + 1;
    }
};

Guess you like

Origin blog.csdn.net/m0_63743577/article/details/128472696