In the Count Asterisks interview question, you are given a string s that contains lowercase letters, vertical bars |, and asterisks *. The vertical bars come in pairs. You need to count the total number of asterisks in the string, but you must exclude any asterisks that are located between a pair of vertical bars.
Google uses the Count Asterisks coding problem as an "Easy" difficulty screening question. it evaluates whether you can process a string while maintaining a simple boolean state (e.g., insidePair). It tests your ability to follow logical constraints and implement a single-pass solution.
This follows a State Machine / Flag pattern.
count = 0 and a boolean flag inside = false.|, flip the inside flag.* and inside is false, increment the counter.Input: s = "l|*e*et|c**o|*de|"
l: Skip.|: inside = true.*: Skip (because inside is true).e: Skip.*: Skip.e, t: Skip.|: inside = false.c: Skip.*: count = 1.*: count = 2.
Result: 2.s.split('|') and then handling the logic, which can be more confusing than a simple loop.This problem is a simplified version of "Parsing" (like checking for balanced parentheses). Practice using flags to track context while scanning linear data.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Flip Game | Easy | Solve | |
| Generate a String With Characters That Have Odd Counts | Easy | Solve | |
| Goal Parser Interpretation | Easy | Solve | |
| License Key Formatting | Easy | Solve | |
| Occurrences After Bigram | Easy | Solve |