Count Substrings with Almost-Even Letter Counts

Quick Overview

Count Substrings with Almost-Even Letter Counts. The string word uses letters a through j. Count nonempty contiguous substrings in which at most one distinct letter has an odd frequency. Count occurrences by position, even when their text is identical.

Count Substrings with Almost-Even Letter Counts

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Count the nonempty contiguous substrings of `word` in which no more than one letter occurs an odd number of times. The input uses only lowercase letters `a` through `j`. Count each occurrence separately, even when two substrings contain identical text. ```cpp class Solution { public: long long wonderfulSubstrings(string word); }; ``` ### Examples **Example 1:** ```text word = "ccc" Output: 6 Explanation: All three length-one, both length-two, and the length-three substring qualify. ``` **Example 2:** ```text word = "abc" Output: 3 Explanation: Only the three single-character substrings have at most one odd count. ``` ### Constraints - `1 <= word.length <= 10^5`. - Characters belong to the first ten lowercase English letters, a through j. ### Clarifying Questions Zero odd-frequency letters is allowed. Two equal substring texts at different positions contribute separately. ```hint Inspect the structure How does XOR of two prefix parity masks describe the substring between them? ``` ### Reasoning checkpoints - Count exactly the substrings meeting the odd-frequency condition, with no empty substring or duplicate counting of one occurrence. - Handle repeated letters and a result that can exceed a signed 32-bit integer. ### Optional extensions How would expanding the permitted alphabet to 26 letters affect time and memory requirements?

Overview: Count Substrings with Almost-Even Letter Counts. The string word uses letters a through j. Count nonempty contiguous substrings in which at most one distinct letter has an odd frequency. Count occurrences by position, even when their text is identical.

Uber logo
Uber
May 28, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Count the nonempty contiguous substrings of word in which no more than one letter occurs an odd number of times. The input uses only lowercase letters a through j. Count each occurrence separately, even when two substrings contain identical text.

class Solution {
public:
    long long wonderfulSubstrings(string word);
};

Examples

Example 1:

word = "ccc"
Output: 6
Explanation: All three length-one, both length-two, and the length-three substring qualify.

Example 2:

word = "abc"
Output: 3
Explanation: Only the three single-character substrings have at most one odd count.

Constraints

  • 1 <= word.length <= 10^5 .
  • Characters belong to the first ten lowercase English letters, a through j.

Clarifying Questions Guidance

Zero odd-frequency letters is allowed. Two equal substring texts at different positions contribute separately.

Reasoning checkpoints

  • Count exactly the substrings meeting the odd-frequency condition, with no empty substring or duplicate counting of one occurrence.
  • Handle repeated letters and a result that can exceed a signed 32-bit integer.

Optional extensions

How would expanding the permitted alphabet to 26 letters affect time and memory requirements?