Collapse Runs of Repeated Characters
Updated
Problem statement
You are given a string s of lowercase English letters. A run is a group of one or more equal letters that stand next to each other. Replace every run with a single copy of its letter and return the new string.
Only neighbors count. If a letter shows up again after a different letter, it starts a new run, so it appears again in the answer.
Function
collapseRuns(s: String) → StringExamples
Example 1
s = "bbbookkeeper"return = "bokeper"The runs are bbb, oo, kk, ee, p, e and r. Each run becomes one letter, so the answer is "bokeper". The second e stays because a p sits between it and the first group of es.
Example 2
s = "mississippi"return = "misisipi"The pairs ss, ss and pp each become one letter. Every other letter is already a run of length 1, so the answer is "misisipi". The letter s appears twice in the answer because the two pairs are separate runs.
Example 3
s = "zzzz"return = "z"The whole string is one run of z, so the answer is "z".
Constraints
1 <= s.length <= 10^5sconsists only of lowercase English letters (atoz).
More Nutanix Onsite Interview questions
- Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
- Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
- Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
- Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
- LRU CacheONSITE INTERVIEW · Seen Aug 2025
- Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
- Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
- Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
- Sudoku SolverONSITE INTERVIEW · Seen Feb 2022
- Distance Sums to Special Nodes in a Weighted TreeONSITE INTERVIEW · Seen Aug 2021
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