Count Log Substrings With at Most One Odd-Count Letter
Last updated: November 29, 2026
Quick Overview
Given a string over the letters 'a' to 'j' (for example compressed event codes), count the non-empty substrings in which at most one letter occurs an odd number of times.
0
0
0 solved
Given a string over the letters 'a' to 'j' (for example compressed event codes), count the non-empty substrings in which at most one letter occurs an odd number of times.
A Google onsite problem that tests whether you can combine prefix parity bitmasks with a hash map, framed around scanning compressed event-code logs for near-palindromic patterns (any such substring can be rearranged into a palindrome). The interviewer looks for the O(n * alphabet) counting idea and a precise argument for the one-bit-differs case.
What the Interviewer Expects
- Represents letter parity of each prefix as a 10-bit mask
- Recognizes equal masks mean all letters even in between
- Counts masks differing by one bit for the single-odd case
- Counts with a frequency table of previous masks
- Achieves O(n * 10) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Count substrings that can be rearranged into a palindrome of even length only.
- Find the longest such substring instead of counting.
- Extend to all 26 letters; does the approach still work?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: string
sof length up to 10^5 with charactersatoj. - Output: number of substrings (by position) with at most one letter of odd count.
- Equivalently, substrings whose letters can be ...
High-Level Design
Let mask[i] be the parity of each letter's count in the first i characters, packed into 10 bits. The substring s[i:j] has parity mask[j] ^ mask[i]. It qualifies if that XOR is 0 (all even) or ha...