[코테 풀이] Remove Palindromic Subsequences

시내·2024년 6월 28일

Q_1332) Remove Palindromic Subsequences

출처 : https://leetcode.com/problems/remove-palindromic-subsequences/

You are 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. Note that a subsequence does not necessarily need to be contiguous.

A string is called palindrome if is one that reads the same backward as well as forward.

class Solution {
     public int removePalindromeSub(String s) {
        if (isPalindrome(s)) return 1;
        else return 2;
    }


    public boolean isPalindrome(String s) {
        for (int i = 0; i < s.length() / 2; i++) {
            if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }
}

🙈 문제 풀이 참조해서 푼 문제

1) s 가 palindrome 이라면 한 번에 empty string으로 만들 수 있다 -> return 1

2) 주어진 string s 가 오직 ab로 이루어져 있기 때문에 s 가 palindrome이 아니더라도 최대 2번에 걸쳐서 empty string으로 만들 수 있다 -> return 2

profile
contact 📨 ksw08215@gmail.com

0개의 댓글