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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code