Tokenizer Builder - Problem

Build a tokenizer that breaks a math expression string into tokens: numbers, operators, and parentheses.

A math expression contains:

  • Numbers: Integer sequences like 123, 45
  • Operators: +, -, *, /
  • Parentheses: ( and )

Return an array of tokens in the order they appear. Ignore spaces between tokens.

Input & Output

Example 1 — Basic Expression
$ Input: expression = "3 + 45 * (2)"
Output: ["3", "+", "45", "*", "(", "2", ")"]
💡 Note: Numbers (3, 45, 2), operators (+, *), and parentheses are separated into individual tokens. Spaces are ignored.
Example 2 — No Spaces
$ Input: expression = "123+456"
Output: ["123", "+", "456"]
💡 Note: Multi-digit numbers 123 and 456 are correctly identified as single tokens even without spaces.
Example 3 — Complex Expression
$ Input: expression = "(1+2)*3/4"
Output: ["(", "1", "+", "2", ")", "*", "3", "/", "4"]
💡 Note: All token types: parentheses, single-digit numbers, and all four basic operators.

Constraints

  • 1 ≤ expression.length ≤ 1000
  • expression contains only digits 0-9, operators +, -, *, /, parentheses (, ), and spaces
  • Numbers are positive integers

Visualization

Tap to expand
INPUTALGORITHMRESULTMath Expression"3 + 45 * (2)"Characters:3 + 451Scan each character2Identify token type:• Digit → build number• Operator → single char• Space → skip3Build complete tokens4Add to result arrayToken Array"3""+""45""*""(""2"")"7 tokens extractedNumbers: 3, 45, 2Operators: +, *Parentheses: (, )Key Insight:A tokenizer recognizes meaningful chunks (numbers, operators, symbols) byidentifying character type transitions during a single pass through the string.TutorialsPoint - Tokenizer Builder | Single Pass State Machine
Asked in
Google 35 Microsoft 28 Amazon 22 Apple 18
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen