BackmediumStrings Atlassian

Lost in Text: Recovery of a Corrupted String Solution

Problem Statement

Given a corrupted string, recover the original string by removing leading and trailing characters until a valid word is found. A valid word is a word that consists only of letters. If the remaining string is a valid word, return the first valid word found. If the input string contains only non-alphabetic characters, return an empty string.

Example 1
Input
12345LOST123
Output
LOST

Explanation: Step-by-step: 1. Remove leading characters '12345' until a valid word is found. 2. The remaining string is 'LOST', which is a valid word. 3. Return the first valid word found.

Example 2
Input
123RECOVER123
Output
RECOVER

Explanation: Step-by-step: 1. Remove leading characters '123' until a valid word is found. 2. The remaining string is 'RECOVER123', which is not a valid word. 3. Remove trailing characters '123' until a valid word is found. 4. The remaining string is 'RECOVER', which is a valid word. 5. Return the first valid word found.

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 Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free