In the Find Longest Awesome Substring coding problem, a string is considered "awesome" if its characters can be rearranged to form a palindrome. You are given a string of digits and need to find the length of the longest awesome contiguous substring. A string can form a palindrome if at most one character appears an odd number of times.
Google uses this "Hard" problem to test a candidate's ability to combine Bit Manipulation with the Prefix Sum interview pattern. It evaluations whether you can represent the state of character parities using a bitmask and use a Hash Table to find the earliest occurrence of a state. It’s a highly efficient solution to a problem that initially looks like it requires checks.
This problem uses Bitmasking and Prefix Parity.
firstSeen with mask 0 at index -1.currentMask using XOR for each digit.firstSeen[prevMask] to current awesome:
currentMask == prevMask (all characters appear even times).currentMask ^ prevMask has exactly one bit set (one character appears odd times). Check this by XORing the currentMask with each of the 10 possible single-bit masks.i - firstSeen[targetMask].String: "324241"
mask 0 at -1.0000001000. firstSeen[mask] = 0.0000001100. firstSeen[mask] = 1.Bitmasks are the standard way to track "parity" for a small number of categories (like 10 digits or 26 letters). Combining this with a Hash Map to store the "first appearance" of each mask is a recurring pattern for "Longest Subarray with Parity Condition" problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Palindrome Permutation | Easy | Solve | |
| Binary String With Substrings Representing 1 To N | Medium | Solve | |
| Number of Good Ways to Split a String | Medium | Solve | |
| Find the Longest Substring Containing Vowels in Even Counts | Medium | Solve | |
| Number of Wonderful Substrings | Medium | Solve |