LC 3844 - Longest Almost-Palindromic Substring

I tried to start from the center with i,i and i,i+1, and expand from both side. I use ignore to maintain if we need to expand from left or right, and use a while loop to do the expansion. When the character is mistmatch, we try to ignore the left (or right) and kept matching. Notice that the ending condition for the while loop is one of the l and r pointer is out of bound, in this case, we may need to check if the ignore is not used and we can do one more futher expansion, we can add one to the length. ...

February 21, 2026 · 2 min

LC 3843 - First Element with Unique Frequency

I count the freuqency for each number, and do reverse for the hashmap to map frequency to numbers, and stored the numbers that occured only once, and search the first occurence of this number. Actually,we can have a more simple version (in Code-2), to further store the count for each frequency in freqCnt, and we only need to do dobule hashmap when searching again in nums. Code-1: class Solution { public: int firstUniqueFreq(vector<int>& nums) { unordered_map<int, int> counts; for(const auto& num: nums) { ++counts[num]; } unordered_map<int, vector<int>> numOfCnt; for(auto [num, cnt]: counts) { if(numOfCnt.count(cnt)) { numOfCnt[cnt].push_back(num); } else { numOfCnt[cnt] = {num}; } } unordered_set<int> uniq; for(auto [cnt, v]: numOfCnt) { if(v.size() > 1) continue; uniq.insert(v[0]); } if(uniq.size() == 0) return -1; for(const auto& num: nums) { if(uniq.count(num)) return num; } return -1; } }; Code-2: Simplified by ChatGPT class Solution { public: int firstUniqueFreq(vector<int>& nums) { unordered_map<int,int> cnt; for (int x : nums) ++cnt[x]; unordered_map<int,int> freqCnt; // freq -> how many numbers have this freq for (auto &p : cnt) ++freqCnt[p.second]; for (int x : nums) { if (freqCnt[cnt[x]] == 1) return x; } return -1; } };

February 21, 2026 · 1 min

LC 3842 - Toggle Light Bulbs

Maintain the state for each index in stat, and return the on one in the end. Code class Solution { public: vector<int> toggleLightBulbs(vector<int>& bulbs) { vector<bool> stat(101, false); for(const auto& b: bulbs) { stat[b] = !stat[b]; } vector<int> ans; for(int i = 1 ; i <= 100; ++i) { if(stat[i]) ans.push_back(i); } return ans; } };

February 21, 2026 · 1 min