Run-Length Encode a String
Updated
Problem statement
Given a string s, replace every maximal run of equal consecutive characters with the character followed by the run's decimal length.
Concatenate the encoded runs in their original order and return the resulting string. Return an empty string when s is empty.
Function
runLengthEncode(s: String) → StringExamples
Example 1
s = "aaabbc"return = "a3b2c1"The maximal runs are aaa, bb, and c, with lengths 3, 2, and 1.
Example 2
s = "abcd"return = "a1b1c1d1"Every character forms a run of length one.
Example 3
s = ""return = ""There are no runs to encode.
Constraints
0 <= s.length <= 200000scontains lowercase English letters.
More Google Phone Screen questions
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026
- Minimum Total Layover WaitPHONE SCREEN · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Portfolio Valuation, Part 1Stripe · PHONE SCREENStripe interview questions
- Format a Signed Integer with CommasHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Run-Length DecodingApple · PHONE SCREENApple interview questions
- Trace Pointer Rebinding and Pointee UpdatesArista Networks · PHONE SCREENArista Networks interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Decode and Store StringsAirbnb · PHONE SCREENAirbnb interview questions
- Delimiter-Parsing String Iteratoramo · PHONE SCREEN