2dbi

Extract continuous number sequences from strings

viaLeetCode

Problem Given a list of strings containing letters and digits, extract every maximal run of consecutive digits from each string and return all such numbers.

Input / Output

  • Input: string array, e.g. ["12A", "2VE3", "24"].
  • Output: list of numbers in order of appearance, e.g. [12, 2, 3, 24].

Constraints

  • Runs can be multi-digit; watch leading zeros ("007" → 7 or "007"? — clarify) and very long runs that overflow int (return as strings/BigInteger if so).

Example

  • ["12A", "2VE3", "24"] → [12, 2, 3, 24]
  • ["a1b22c333"] → [1, 22, 333]

Expected approach

  • Single pass per string: accumulate while the current char is a digit; on a non-digit (or end of string), flush the accumulated run to the result. O(total characters) time. A regex (\d+) is acceptable but be ready to code the manual scan and discuss the edge cases above.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account