Run-Length Encoding
Updated
Problem statement
Given a string text, encode each maximal run of equal consecutive characters from left to right.
For every run, append the run's character followed by the full run length in base 10. Include the count 1 for a singleton run.
Return the concatenated encoding. Return an empty string when text is empty.
Function
runLengthEncode(text: String) → StringExamples
Example 1
text = "aaabbccccd"return = "a3b2c4d1"The runs are aaa, bb, cccc, and d. Every run contributes its count.
Example 2
text = ""return = ""An empty input has no runs.
Example 3
text = "zzzzzzzzzzzzx"return = "z12x1"The first run shows a multi-digit count, and the singleton x still contributes 1.
Constraints
0 <= text.length <= 200000textcontains printable ASCII characters.
More Zscaler interview questions
- Asteroid CollisionONSITE INTERVIEW · Seen Sep 2024
- Check Whether a Byte Is NonzeroONSITE INTERVIEW · Seen Sep 2024
- Count Vowel PermutationsONSITE INTERVIEW · Seen Sep 2024
- Longest Common Subsequence LengthONSITE INTERVIEW · Seen Jun 2024
- Rate Limiter with Hourly and Daily Effort QuotasPHONE SCREEN · Seen Jul 2026
- Merging PalindromesPHONE SCREEN · Seen Sep 2024
- Find the Element With Odd FrequencyPHONE SCREEN · Seen Jun 2024
- Merge IntervalsPHONE SCREEN · Seen Jun 2024
- Sum of Divisors of the Array GCDPHONE SCREEN · Seen Jun 2024
- How Many Sentences? 🌿OA · Seen Mar 2024
Similar questions at other companies
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Reverse Vowels of a StringGoogle · ONSITE INTERVIEWGoogle interview questions
- Valid Palindrome After NormalizationKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Count Binary SubstringsDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Palindrome Match PercentageApple · ONSITE INTERVIEWApple interview questions
- Reverse A StringNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Validate a Word AbbreviationSalesforce · ONSITE INTERVIEWSalesforce interview questions