LC 647 - Palindromic Substrings

Check the max expansion for each odd and even index, and sum then up. Code class Solution { public: int countSubstrings(string s) { int n = s.size(); int totalCnt = 0; for(int i = 0 ; i < n ; ++i) { totalCnt += expand(s, i, i); totalCnt += expand(s, i, i+1); } return totalCnt; } int expand(const string& s, int l, int r) { int n = s.size(); int cnt = 0; while(l >= 0 && r <= n-1 && s[l] == s[r]) { --l; ++r; ++cnt; } return cnt; } };

February 21, 2026 · 1 min

LC 91 - Decode Ways

Match the currnet idx as one or two-digit and recursively do this, also maintain a memo to avoid re-calculation. Also, originally I’m using stoi(s.substr(s, 2)), where the chatGPT recommends to use (s[idx]-'0)*10 + s[idx+1] -'0', and I find that this approach is much faster. Code class Solution { public: int numDecodings(string s) { memo.assign(s.size(), -1); return decoding(s, 0); } int decoding(const string& s, int idx) { int n = s.size(); if(idx > n-1) return 0; int totalCnt = 0; if(memo[idx] != -1) return memo[idx]; if(s[idx] != '0') { totalCnt += idx == n-1 ? 1 : decoding(s, idx+1); } if( idx <= n-2 && (s[idx]-'0')*10 +(s[idx+1]-'0') <= 26 && s[idx] != '0') { totalCnt += idx == n-2 ? 1 : decoding(s, idx+2); } memo[idx] = totalCnt; return totalCnt; } vector<int> memo; };

February 21, 2026 · 1 min

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

LC 696 - Count Binary Substrings

I count the consecutive 1’s and 0’s, and for each transition, we take the minimum number of 1’s and 0’s, and this is the number of words that can be formed in this transtiion. Code class Solution { public: int countBinarySubstrings(string s) { int n = s.size(); vector<int> cnt; cnt.reserve(n); int zero = 0; int one = 0; for(int i = 0 ; i < n ; ++i) { if(s[i] == '1') { if(zero != 0) { cnt.push_back(zero); zero = 0; } ++one; } else { if(one != 0) { cnt.push_back(one); one = 0; } ++zero; } } if(zero != 0) cnt.push_back(zero); if(one != 0) cnt.push_back(one); int m = cnt.size(); int totalCnt = 0; for(int i = 1 ; i < m ; ++i) { totalCnt += min(cnt[i-1], cnt[i]); } return totalCnt; } };

February 19, 2026 · 1 min

LC 3719 - Longest Balanced Subarray I

Approach Do it in O(N^2) that count all the even and odd number count in each range [i .. j] with the help of hash, which would only store once for duplicate numbers. Code class Solution { public: int longestBalanced(vector<int>& nums) { int n = nums.size(); int maxLen = 0; for(int i = 0 ; i < n ; ++i) { unordered_set<int> even; unordered_set<int> odd; for(int j = i ; j < n ; ++j) { if(nums[j] % 2 == 1) odd.insert(nums[j]); else even.insert(nums[j]); if(even.size() == odd.size()) maxLen = max(maxLen, j-i+1); } } return maxLen; } };

February 10, 2026 · 1 min

LC 1382 - Balance a Binary Search Tree

Approach To rebuild the balacne tree, I stored the inorder traversal in the vector, and then break all the left and right connection (this step is important!), and then connecting them again by spliting the nodes into two clusters having similar number of nodes. Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* balanceBST(TreeNode* root) { vector<TreeNode*> inorder; traverse(inorder, root); int n = inorder.size(); for(TreeNode* node: inorder) node->left = node->right = nullptr; return rebuild(inorder, 0, n-1); } void traverse(vector<TreeNode*>& inorder, TreeNode* root) { if(root->left) traverse(inorder, root->left); inorder.push_back(root); if(root->right) traverse(inorder, root->right); } TreeNode* rebuild(const vector<TreeNode*>& inorder, int start, int end) { if(start == end) return inorder[start]; if(end < start) return nullptr; int m = start + (end - start) / 2; TreeNode* curNode = inorder[m]; curNode->left = rebuild(inorder, start, m-1); curNode->right = rebuild(inorder, m+1, end); return curNode; } };

February 9, 2026 · 1 min

LC 338 - Counting Bits

Approach Need to couting bits for each n, and this is actually the number of bits for n/2 and plusing n%2. Code class Solution { public: vector<int> countBits(int n) { if(n == 0) return {0}; if(n == 1) return {0, 1}; vector<int> ans(n+1); ans[0] = 0; ans[1] = 1; for(int i = 2 ; i <= n ; ++i) { ans[i] = ans[i/2] + (i % 2); } return ans; } };

February 8, 2026 · 1 min

LC 1653 - Minimum Deletions to Make String Balanced

In this problme we need to removal character from string aab...bab containing only a and b, and make sure in the end it becomes aa..aab...b, find the minimum removal. I tried to count the prefix for how many b presents, and postifx for how many a presents, then when we want to seperate the string into two clusters, [.. idx] and [idx+1 ..], and make the first clsuter all a, and the second all b, we only need to add up prefBandpostA`. we check and maintan the best answer. ...

February 7, 2026 · 1 min