zoukankan      html  css  js  c++  java
  • 1332. Remove Palindromic Subsequences

    package LeetCode_1332
    
    /**
     * 1332. Remove Palindromic Subsequences
     * https://leetcode.com/problems/remove-palindromic-subsequences/
     *
     * Given a string s consisting only of letters 'a' and 'b'.
     * In a single step you can remove one palindromic subsequence from s.
    Return the minimum number of steps to make the given string empty.
    A string is a subsequence of a given string, if it is generated by deleting some characters of a given string without changing its order.
    A string is called palindrome if is one that reads the same backward as well as forward.
    
    Example 1:
    Input: s = "ababa"
    Output: 1
    Explanation: String is already palindrome
     * */
    class Solution {
        /*
        * solution: check string if Palindromic, if yes, use 1 time, 
        * else because subsequence don't have to be consecutive, and just contains a,b, so can be delete up to 2 times
        * */
        fun removePalindromeSub(s: String): Int {
            var i = 0
            var j = s.length
            while (i < j) {
                if (s[i] != s[j]) {
                    //not Palindromic
                    return 2
                }
                i++
                j--
            }
            return 1
        }
    }
  • 相关阅读:
    90.子类调用父类同名方法总结
    89.子类调用父类同名属性和方法
    88.子类调用父类同名属性和方法
    87.子类重写父类的同名属性和方法
    86.多继承
    85.单继承
    84.继承的概念
    day09
    83.魔法方法__del__()
    82.魔法方法__str__()
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13935968.html
Copyright © 2011-2022 走看看