Problem · String
Better Compression
Learn this problemProblem statement
Consider a string, S, that is a series of characters, each followed by its frequency as an integer.
The string is not compressed correctly, so there may be multiple occurrences of the same character.
A properly compressed string will consist of one instance of each character in alphabetical order followed by the
total count of that character within the string.
Function
betterCompression(S: String) → StringComplete the function betterCompression in the editor.
betterCompression has the following parameter:
S: a compressed stringReturn
string: the properly compressed stringExamples
Example 1
S = "a3c9b2c1"return = "a3b2c10"The string 'a3c9b2c1' has two instances where 'c' is followed by a count: once with 9 occurrences, and again with 1. It should be compressed to 'a3b2c10'.
Example 2
S = "a12b56c1"return = "a12b56c1"Noting is changed because character occurred only once and they are already sorted ascending.
Constraints
1 <= size of S <= 100000'a' <= characters in S <= 'z'1 <= frequency of each character in S <= 1000More Goldman Sachs problems
- Data ReorganizationSeen Jul 2026
- Inherited Role PermissionsONSITE INTERVIEW · Seen Jul 2026
- Root of the Largest TreePHONE SCREEN · Seen Jul 2026
- Validate Binary Search TreeONSITE INTERVIEW · Seen Jul 2026
- Alternating Parity PermutationsOA · Seen Jul 2026
- Threshold AlertsSeen Jul 2026
- Cheapest Flights Within K StopsONSITE INTERVIEW · Seen Jun 2026
- Word LadderPHONE SCREEN · Seen Jun 2026