mediumStringsPattern: Mixed
String Reconstruction from Character Counts Solution
Problem Statement
Given a string of characters and their respective counts, reconstruct the original string if possible, otherwise return an empty string. The counts are represented as a list of pairs, where each pair contains a character and its count.
Examples
Example 1:
Input:[['a', 2], ['b', 1], ['c', 3]]
Output:aabccc
Explanation: The character 'a' appears 2 times, 'b' appears 1 time, and 'c' appears 3 times, so the reconstructed string is 'aabccc'.
Example 2:
Input:[['x', 1], ['y', 2], ['z', 0]]
Output:xyy
Explanation: The character 'x' appears 1 time, 'y' appears 2 times, and 'z' appears 0 times, so the reconstructed string is 'xyy'.
Constraints
- The length of the input list is at most 26, representing the 26 English letters.
- The count of each character is a non-negative integer.
- The total count of all characters is at most 10^5.
Time: O(n) Space: O(n)
A more efficient approach is to use a single loop to iterate over the sorted character counts and append each character to the result string the specified number of times, resulting in a time complexity of O(n).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
