BackmediumStringsAmazonRazorpay

Corrupted String Recovery Solution

Problem Statement

Given a corrupted string with '#' and '' characters, write a function to recover the original string by removing all '#' and '' characters. The function should handle null or undefined input.

Example 1
Input
He#lo
Output
He

Explanation: Step-by-step: Given the input string 'He#lo', we first remove all '#' characters, resulting in 'Heo'. Then, we remove all '*' characters, but since there are none, the final output is 'He'.

Example 2
Input
H##*ello
Output
Helo

Explanation: Step-by-step: Given the input string 'H##*ello', we first remove all '#' characters, resulting in 'He*ello'. Then, we remove all '*' characters, resulting in 'Helo'.

Constraints

  • The corrupted snapshot is a non-empty string of lowercase English letters and digits.
  • The original string is a valid English word.
  • The length of the corrupted snapshot does not exceed 30 characters.
  • The dictionary of valid words is provided as a set of words.
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

Corrupted String Recovery — Problem Statement & Solution Guide

StringsMediumRECOVER 1777831512659
TimeO(n)
|
SpaceO(n)

Problem Description

Given a corrupted string with '#' and '*' characters, write a function to recover the original string by removing all '#' and '*' characters. The function should handle null or undefined input.

Examples

Example 1

Input

He#lo

Output

He

Explanation: Step-by-step: Given the input string 'He#lo', we first remove all '#' characters, resulting in 'Heo'. Then, we remove all '*' characters, but since there are none, the final output is 'He'.

Example 2

Input

H##*ello

Output

Helo

Explanation: Step-by-step: Given the input string 'H##*ello', we first remove all '#' characters, resulting in 'He*ello'. Then, we remove all '*' characters, resulting in 'Helo'.

Constraints

  • The corrupted snapshot is a non-empty string of lowercase English letters and digits.
  • The original string is a valid English word.
  • The length of the corrupted snapshot does not exceed 30 characters.
  • The dictionary of valid words is provided as a set of words.

Optimal Approach & Strategy

Optimal approach involves using a set of valid words and scanning the corrupted string for word boundaries, resulting in a linear time complexity of O(n) with O(26) space complexity.

Brute Force Approach

Naive approach would involve checking every substring of the corrupted string against the dictionary of valid words, resulting in an O(n^2) time complexity.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function recoverString(s) { if (!s) return ''; let result = ''; for (let i = 0; i < s.length; i++) { if (s[i] === '#' && i > 0 && s[i - 1] === '*') { i--; continue; } if (s[i] !== '#' && s[i] !== '*') result += s[i]; } return result; }

Asked in Top Tech Interviews

AmazonRazorpay

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.