LC 3847 - Find the Score Difference in a Game

Check if the current number is odd, and the idx is mod 5 by 6, then update score1 and score2 Code class Solution { public: int scoreDifference(vector<int>& nums) { int score1 = 0; int score2 = 0; bool active1 = true; int n = nums.size(); for(int i = 0 ; i < n ; ++i) { if((nums[i] % 2 == 1) ^ (i % 6 == 5)) active1 = !active1; if(active1) score1 += nums[i]; else score2 += nums[i]; } return score1 - score2; } };

February 22, 2026 · 1 min

LC 3848 - Check Digitorial Permutation

We pre-calculate the factorial for 1 to 10 first, then iterate thorught the string and add all the factorial up. Now we sum then up, and chekc if this sum can be formed by the original numers digits. Code class Solution { public: bool isDigitorialPermutation(int n) { vector<int> fac(10, 1); for(int i = 1 ; i < 10 ; ++i) fac[i] = fac[i-1] * i; string s = to_string(n); int cnt = s.size(); int sum = 0; for(int i = 0 ; i < cnt ; ++i) { sum += fac[s[i] - '0']; } string s2 = to_string(sum); if(s2.size() != s.size()) return false; unordered_map<char, int> charCnt; for(const auto& c: s) ++charCnt[c]; for(const auto& c: s2) { if(charCnt[c] == 0) return false; --charCnt[c]; } return true; } };

February 22, 2026 · 1 min

LC 3849 - Maximum Bitwise XOR After Rearrangement

We record the position for ones and zeros, then count the number of ones in t, the goal is to match the 1’s in t to some positions in s. The stategy is to (1) mathc the zeros from MSB, and (2) match the zeros from LSB, since matching zeros would increase the value, so we want to do this from MSB, and for mathcing ones we decreas the value so we want to do this from LSB. ...

February 22, 2026 · 2 min

LC 868 - Binary Gap

Store the binary representaiton and then find the max distance betweeb location of 1’s. Code class Solution { public: int binaryGap(int n) { deque<char> s; while(n > 0) { if(n % 2 == 1) s.push_front('1'); else s.push_front('0'); n /= 2; } vector<int> idx; for(int i = 0 ; i < s.size() ; ++i) if(s[i] == '1') idx.push_back(i); if(idx.size() <= 1) return 0; int maxWidth = 1; for(int i = 1 ; i < idx.size() ; ++i) maxWidth = max(maxWidth, idx[i] - idx[i-1]); return maxWidth; } };

February 22, 2026 · 1 min

LC 3850 - Count Sequences to K

To check whether it can become an integer k, we track prime-exponent vectors instead of doing fraction arithmetic. In this problem we only care about primes {2,3,5} since the number is 1 to 6. If k contains any other prime factor, it is impossible, return 0. Then we do the search backwards, process nums from right to left, and at each position, do unchanged, multiple or divide. When we finish all indices we add the total count by one if the remaining target is 1. ...

February 22, 2026 · 3 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 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 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 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 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