Problem · String
Run-Length String Compression
Learn this problemProblem statement
Given a string s, compress each maximal group of consecutive equal characters.
- Append the group's character once.
- If the group contains more than one character, append its full length in base 10.
- If the group contains exactly one character, do not append a count.
Return the compressed string.
Function
compressString(s: String) → StringExamples
Example 1
s = "aaaabeee"return = "a4be3"The groups are aaaa, b, and eee. Their encodings are a4, b, and e3.
Example 2
s = "a"return = "a"The only group has length 1, so no count is appended.
Example 3
s = "abbbbbbbbbbbb"return = "ab12"The first group contributes a. The following group contains 12 copies of b, so it contributes b12.
Constraints
1 <= s.length <= 2000scontains letters, digits, or symbols.
More Salesforce problems
- Diameter of an Acyclic Undirected GraphONSITE INTERVIEW · Seen Jul 2026
- Optimal Account BalancingPHONE SCREEN · Seen Jul 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Jul 2026
- Maximal SquarePHONE SCREEN · Seen Jul 2026
- Maximum Barbell WeightOA · Seen Jul 2026
- Minimum No-Repeat Segments After One Character RemovalOA · Seen Jul 2026
- Minimum Operations to ZeroOA · Seen Jul 2026
- Minimize Total Input Cost (for LTMS)OA · Seen Jun 2026