LC 3805 - Count Caesar Cipher Pairs

Approach Try to let the first char to become ‘a’, and then compare how many strings are the same for each one (with hashmap). Code class Solution { public: long long countPairs(vector<string>& words) { int m = words.size(); int n = words[0].size(); for(int i = 0 ; i < m ; ++i) { int shift = words[i][0] - 'a'; for(int j = 0 ; j < n ; ++j) { int curShift = (int(words[i][j] - 'a') - shift + 26 ) % 26; words[i][j] = ('a' + curShift); } } unordered_map<string, int> mp; long long totalCnt = 0; for(int i = 0 ; i < m ; ++i) { if(mp.count(words[i])) totalCnt += mp[words[i]]; ++mp[words[i]]; } return totalCnt; } };

January 11, 2026 · 1 min

LC 3804 - Number of Centered Subarrays

Approach problem 2 can also be done using hash and prefixsum, timing complexity is O(N^2) Code class Solution { public: int centeredSubarrays(vector<int>& nums) { int n = nums.size(); int totalCnt = 0; for(int i = 0 ; i < n ; ++i){ unordered_set<int> st; int sum = 0; for(int j = i ; j < n ; ++j) { sum += nums[j]; st.insert(nums[j]); if(st.count(sum)) ++totalCnt; } } return totalCnt; } };

January 11, 2026 · 1 min

LC 3803 - Count Residue Prefixes

Approach this weeks weekly, problem 1 is simple, we maintain unordered_set to check how many unqiue values and chekc hte current length. Code class Solution { public: int residuePrefixes(string s) { unordered_set<char> st; int resi = 0; for(int i = 0 ; i < s.size() ; ++i) { st.insert(s[i]); if(st.size() == (i+1) % 3) ++ resi; } return resi; } };

January 11, 2026 · 1 min