Day 18 of Days of Dsa

Day 18 of Days of Dsa

ยท

1 min read

In my opinion, it's important to acknowledge and learn from failures. While tackling the daily LeetCode problem, my task was to find all unique palindromes of length three that are subsequences of the given string, 's.' Initially, I formulated a logic based on this requirement. However, my solution turned out to be incorrect, passing only 47 out of 70 test cases. Despite this setback, I believe my approach accurately returns the unique number of palindromic substrings, at least that's my perspective.

Here's the code snippet I implemented:

class Solution {
public:
    int countPalindromicSubsequence(string s) {
        map<char, int> mp;
        for (int i = 0; i < s.size(); i++) {
            mp[s[i]]++;
        }
        int size = mp.size();
        int cnt = 0;
        for (auto it : mp) {
            if (it.second == 2) {
                cnt += (size - 1);
            } else if (it.second > 2) {
                cnt += size;
            }
        }
        return cnt;
    }
};

Upon reflection, I plan to revisit and refine my solution tomorrow. Having just returned from a trip, I'm a bit fatigued, and I believe a fresh perspective might lead to a more accurate and efficient solution.

ย