Stream processing · Easy
A text diagnostic stores consecutive repeats as character-count pairs. It preserves the exact original text order so the text can be reconstructed. Equal characters separated by another character belong to different runs.
Return consecutive character runs as ordered pairs of the character and its run length.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ text: string }
Fields
- text is an ASCII string. Case, punctuation and spaces are significant; do not normalize or trim it.
Output
Return value
- Return [[character,count], ...] in original run order. Empty text returns [].
- Do not merge nonadjacent runs, change case, or discard whitespace.
Constraints
- 0 <= text.length <= 100000; ASCII only.
Examples
Example 1
{
"text": "aaBBa"
}Returns
[
[
"a",
2
],
[
"B",
2
],
[
"a",
1
]
]Example 2
{
"text": " x"
}Returns
[
[
" ",
2
],
[
"x",
1
]
]Required complexity
O(n) time and O(r) output space for r runs