LeetCode brush mention notes 1332 title

Subject description:

You give a string s, it is only by the letter 'a' and 'b' composition. Each time a delete operation can be removed from s the palindromic sequence .

Delete to delete the minimum number of return given string all the characters (the string is empty) of.

"Subsequence" is defined: if a character string can not change the original order by removing some characters get the original string, the string is a sequence of the original string.

"Palindrome" is defined: if a string backward and forward reading is consistent, then the string is a palindrome.

 Topic Source: stay button (LeetCode)

Problem-solving ideas:

Many confusing this subject, because if the substring and subsequence this concept will not distinguish the words are two difficult issues, this topic is a sub-sequence, so much easier, because less required sequence, or else just scrambled original string, you can freely withdrawn character, divided into three cases:

  1. The original string is itself a palindrome string, then only need to clear once to delete all of the characters, the returned result is 1

  2. The original character is not a palindrome string, because the string is only a and b of these two characters, so you only need to remove all of the two characters can be deleted (first delete all a second time delete all b)

  3. The original string is an empty pass directly back 0

Implementation code as follows:

    public int removePalindromeSub(String s) {
        if (s.length()==0)
            return 0;
        int left = 0;
        int right = s.length()-1;
        char[] chars = s.toCharArray();
        while (left < right){
            if (chars[left++] != chars[right--])
                return 2;

        }
        return 1;
    }

 

Guess you like

Origin www.cnblogs.com/yxym2016/p/12549475.html