BackmediumStringsAtlassian

Lost in Text: Recovery of a Corrupted String Solution

Problem Statement

Given a string s, identify the first maximal contiguous block that consists solely of letters from the English alphabet (both uppercase A‑Z and lowercase a‑z). The block must start at the earliest position where a letter appears and extend rightwards until a non‑letter character or the end of the string is encountered. Return this block as a new string. If s contains no letters, return an empty string.

Example 1
Input
123abc!def
Output
abc

Explanation: Scanning from the start, the first character '1' is not a letter. The scan continues until character 'a' at index 3, which begins a run of letters 'a','b','c'. The next character '!' stops the run, so the extracted substring is "abc".

Example 2
Input
!!HelloWorld42
Output
HelloWorld

Explanation: The first two characters are non‑letters. At index 2 the letter 'H' starts a sequence that continues through 'e','l','l','o','W','o','r','l','d'. The following character '4' is not a letter, ending the block. Hence the result is "HelloWorld".

Example 3
Input
2021_#_2022
Output

Explanation: The string contains digits and symbols only; no alphabetic character is found, so the function returns an empty string.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of printable ASCII characters (code 32‑126)
  • The algorithm should run in O(|s|) time and O(1) additional space
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

Lost in Text: Recovery of a Corrupted String — Problem Statement & Solution Guide

StringsMediumRECOVER 1777831512659
TimeO(n)
|
SpaceO(1)

Problem Description

Given a string s, identify the first maximal contiguous block that consists solely of letters from the English alphabet (both uppercase A‑Z and lowercase a‑z). The block must start at the earliest position where a letter appears and extend rightwards until a non‑letter character or the end of the string is encountered. Return this block as a new string. If s contains no letters, return an empty string.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Lost in Text: Recovery of a Corrupted String"

medium

WHY DOES IT MATTER?

Identifying contiguous alphabetic runs is a building block for tokenization, lexical analysis, and input validation, which appear in compilers, data parsers, and security filters.

OPTIMIZATION CHALLENGE

The insight is that once the first alphabetic character is found, you never need to look back—continuously extending the window until a break point yields the answer in a single pass.

REAL-WORLD CONNECTION

Think of a log‑processing pipeline where you need to extract the first human‑readable identifier from a noisy line; the same linear scan isolates the useful token before discarding the rest.

During an interview, start by stating the early‑exit condition: "Find the first letter, then keep reading until a non‑letter; we can stop immediately after that, guaranteeing linear time."

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem reduces to a single-pass linear scan, a classic example of the two‑pointer or sliding‑window technique where one pointer marks the start of a potential block and the other expands it until a violation occurs. A naive solution might repeatedly search for alphabetic substrings using nested loops or regex matches, leading to O(n²) time on pathological inputs because each character could be re‑examined many times. By recognizing that we only need the *first* maximal block, we can terminate as soon as we encounter the first non‑letter after the initial alphabetic character, guaranteeing O(n) time. This optimal paradigm leverages constant‑time character classification (e.g., ASCII range checks) and avoids auxiliary data structures, keeping auxiliary space to O(1) aside from the output string.

Interview Questions on This Problem

Q1How would you modify the algorithm to return all maximal alphabetic blocks in the string, not just the first one?

Iterate through the string with a single index, start a new block each time you encounter a letter after a non‑letter, collect characters until a non‑letter, store the block, and continue; this still runs in O(n) time and O(k) space for k total letters collected.

Q2What edge cases must you consider when the input string contains Unicode characters beyond ASCII?

You must ensure the character classification correctly handles Unicode letters (e.g., using Unicode-aware functions like Character.isLetter in Java or str.isalpha() in Python) because simple ASCII range checks would misclassify accented letters or other scripts.

Q3If the string length can be up to 10⁸, how would you ensure the solution stays within memory limits?

Process the string as a stream or use a memory‑mapped file, scanning character by character without loading the entire string into memory; only the current block (at most the length of the answer) needs to be stored.

Examples

Example 1

Input

123abc!def

Output

abc

Explanation: Scanning from the start, the first character '1' is not a letter. The scan continues until character 'a' at index 3, which begins a run of letters 'a','b','c'. The next character '!' stops the run, so the extracted substring is "abc".

Example 2

Input

!!HelloWorld42

Output

HelloWorld

Explanation: The first two characters are non‑letters. At index 2 the letter 'H' starts a sequence that continues through 'e','l','l','o','W','o','r','l','d'. The following character '4' is not a letter, ending the block. Hence the result is "HelloWorld".

Example 3

Input

2021_#_2022

Output

Explanation: The string contains digits and symbols only; no alphabetic character is found, so the function returns an empty string.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of printable ASCII characters (code 32‑126)
  • The algorithm should run in O(|s|) time and O(1) additional space

Optimal Approach & Strategy

Perform a single linear scan, start recording at the first letter, and stop at the first non‑letter after it.

Brute Force Approach

Repeatedly search for alphabetic substrings using nested loops or regex, re‑scanning parts of the string multiple times.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function firstLetterBlock(s) {
    let i = 0;
    while (i < s.length && !/[A-Za-z]/.test(s[i])) i++;
    const start = i;
    while (i < s.length && /[A-Za-z]/.test(s[i])) i++;
    return s.substring(start, i);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trimEnd();
console.log(firstLetterBlock(input));

Asked in Top Tech Interviews

Atlassian

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.