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 3634 - Minimum Removals to Balance Array

Approach We sort the array first, and then find where the k times of the current value will be and update the best solution. I think we can do two pointer, or early termination when the current removal is already larger then best. Code class Solution { public: int minRemoval(vector<int>& nums, int k) { int n = nums.size(); int minRemoval = n; sort(nums.begin(), nums.end()); for(int i = 0 ; i < n ; ++i) { long long maxVal = (long long)k * nums[i]; if(maxVal > INT_MAX) maxVal = INT_MAX; int idx = upper_bound(nums.begin(), nums.end(), maxVal) - nums.begin(); int removal = n - idx; minRemoval = min(minRemoval, removal+i); } return minRemoval; } };

February 6, 2026 · 1 min

LC 3814 - Maximum Capacity Within Budget

Approach The problem is to seletc at most 2 datas, and we can seperate this into two problems: select 1 and select 2. For selcet 1, it is really simple, we just get the max capcacity that the cost is smaller then budget. For select 2, we use two pointer to do this in linear time. We sort the data by cost and then try to match from the end. ...

January 18, 2026 · 2 min

LC 3795 - Minimum Subarray Length With Distinct Sum At Least K

Approach This is quite easy to see that is a two pointer type of problem. One difficult part is that for the boundary condition, such as one elemenet only may need some carefull handling (I get WA on that). Also, the sum should remove duplicates so we need to store the count for each variable with a hashmap. Add when there is no this element in the range yet, and remove if this is the last one in the range! (also get 1 WA on this…) ...

January 3, 2026 · 1 min

LC 1358 - Number of Substrings Containing All Three Characters

[translate by ChatGPT] This problem is almost the same as the one from yesterday - it’s about finding substrings that contain characters at least a certain number of times. A two-pointer approach is sufficient to solve it successfully. Code class Solution { public: int numberOfSubstrings(string s) { int n = s.size(); vector<int> cnt(3, 0); int left = 0, right = 0, ans = 0; while(right < n) { ++cnt[s[right]-'a']; while(cnt[0] >= 1 && cnt[1] >= 1 && cnt[2] >= 1) { ans += (n - right); --cnt[s[left]-'a']; ++left; } ++right; } return ans; } };

March 11, 2025 · 1 min

LC 3306 - Count of Substrings Containing Every Vowel and K Consonants II

[translate by ChatGPT] This problem is really difficult. Even after reading the solution, it took me quite a while to realize that a two-pointer approach can be used to solve the “at least K” condition. In the end, we can compute the number of substrings with exactly k by taking the difference between “at least k” and “at least k + 1”. Code class Solution { public: long long countOfSubstrings(string word, int k) { return atLeast(word, k) - atLeast(word, k+1); } long atLeast(const string& word, const int& k) { int n = word.size(); int left = 0; int right = 0; vector<int> cnt(6, 0); long ans = 0; while(right < n) { ++cnt[getIdx(word[right])]; while(cnt[0] > 0 && cnt[1] > 0 && cnt[2] > 0 && cnt[3] > 0 && cnt[4] > 0 && cnt[5] >= k) { ans += (n - right); --cnt[getIdx(word[left])]; ++left; } ++right; } return ans; } inline int getIdx(char c) { switch(c) { case 'a': return 0; case 'e': return 1; case 'i': return 2; case 'o': return 3; case 'u': return 4; default : return 5; } } };

March 10, 2025 · 1 min

LC 2905 - Find Indices With Index and Value Difference II

[translate by ChatGPT] Although my algorithm was already O(n), I realized I was doing a lot of unnecessary work during the process. I didn’t actually need to compute the maximum and minimum for every previous position. As soon as I find a valid answer, I can return immediately, which saves a lot of time. I don’t need to store the maximum number itself — I can just keep track of the index and look it up later. I suspect using a vector to store extra values introduces some overhead. ...

March 7, 2025 · 2 min