BackmediumStringsRazorpay

String Reconstruction from Character Counts Solution

Problem Statement

Given a list of character counts, where each character count is a pair of a character and its count, reconstruct the original string if possible, otherwise return an empty string. The counts are cumulative, meaning each character is added to the result the specified number of times.

Example 1
Input
[['a', 1], ['b', 2]]
Output
ab

Explanation: Step-by-step: Given the input [['a', 1], ['b', 2]], we iterate over the character counts. We add 'a' once to the result because its count is 1. Then, we add 'b' twice to the result because its count is 2. Therefore, the output is 'ab'.

Example 2
Input
[['a', 3], ['b', 2], ['c', 1]]
Output
abcc

Explanation: Step-by-step: Given the input [['a', 3], ['b', 2], ['c', 1]], we iterate over the character counts. We add 'a' three times to the result because its count is 3. Then, we add 'b' twice to the result because its count is 2. Finally, we add 'c' once to the result because its count is 1. Therefore, the output is 'abcc'.

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.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

String Reconstruction from Character Counts — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(n)

Problem Description

Given a list of character counts, where each character count is a pair of a character and its count, reconstruct the original string if possible, otherwise return an empty string. The counts are cumulative, meaning each character is added to the result the specified number of times.

Examples

Example 1

Input

[['a', 1], ['b', 2]]

Output

ab

Explanation: Step-by-step: Given the input [['a', 1], ['b', 2]], we iterate over the character counts. We add 'a' once to the result because its count is 1. Then, we add 'b' twice to the result because its count is 2. Therefore, the output is 'ab'.

Example 2

Input

[['a', 3], ['b', 2], ['c', 1]]

Output

abcc

Explanation: Step-by-step: Given the input [['a', 3], ['b', 2], ['c', 1]], we iterate over the character counts. We add 'a' three times to the result because its count is 3. Then, we add 'b' twice to the result because its count is 2. Finally, we add 'c' once to the result because its count is 1. Therefore, the output is 'abcc'.

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.

Optimal Approach & Strategy

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).

Brute Force Approach

One possible brute-force approach is to use a nested loop structure to generate all possible permutations of the characters and then check if the permutation matches the given character counts. However, this approach is inefficient and has a time complexity of O(n!).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function reconstructString(charCounts) {
  let result = '';
  for (let i = 0; i < charCounts.length; i++) {
    let char = charCounts[i][0];
    let count = charCounts[i][1];
    result += char.repeat(count);
  }
  return result;
}

Asked in Top Tech Interviews

Razorpay

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.