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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
Prefix XOR
Bitmask
Hash Map
Counting Substrings
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: string s of length up to 10^5 with characters a to j.
  • 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...


Submit Your Answer
Markdown supported