반응형

https://leetcode.com/problems/unique-length-3-palindromic-subsequences

 

Unique Length-3 Palindromic Subsequences - LeetCode

Can you solve this real interview question? Unique Length-3 Palindromic Subsequences - Given a string s, return the number of unique palindromes of length three that are a subsequence of s. Note that even if there are multiple ways to obtain the same subse

leetcode.com

 

길이 3의 팰린드롬은, 양 끝 문자가 동일하면 됩니다.

각 문자가 처음/끝에 등장하는 지점을 찾아주고, 해당 지점 사이에서 고유한 문자의 개수를 구해주면 됩니다.

class Solution {
public:
    int countPalindromicSubsequence(string s) {
        int left[26] = {0}, right[26] = {0};

        for(int i=0; i<s.size(); i++) {
            left[s[s.size() - i - 1] - 'a'] = s.size() - i - 1;
            right[s[i] - 'a'] = i;
        }

        int res = 0;
        for(int i=0; i<26; i++) {
            res += countUniqueLetters(s, left[i] + 1, right[i] - 1);
        }
        return res;
    }

private:
    int countUniqueLetters(string& s, int l, int r) {
        unordered_set<char> letters;
        for(int i=l; i<=r; i++) letters.insert(s[i]);
        return letters.size();
    }
};
반응형

'Algorithm' 카테고리의 다른 글

LeetCode 15. 3Sum  (0) 2023.11.15
LeetCode 1846. Maximum Element After Decreasing and Rearranging  (0) 2023.11.15
LeetCode 2785. Sort Vowels in a String  (1) 2023.11.13
LeetCode 815. Bus Routes  (1) 2023.11.12
LeetCode 386. Lexicographical Numbers  (0) 2023.11.11

+ Recent posts