Check Whether String A Can Be Converted to String B by Deleting Characters

Quick Overview

Decide whether one string can be turned into another by deleting characters without reordering the ones that remain. It tests the two-pointer subsequence scan, why matching each character as early as possible is safe, and edge cases with empty strings.

Check Whether String A Can Be Converted to String B by Deleting Characters

Company: Electronic Arts

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two strings `a` and `b`, determine whether `a` can be converted into `b` by removing zero or more characters from `a` without rearranging the remaining characters. Return `True` if `b` appears as a subsequence of `a`, and `False` otherwise. ### Function Signature ```python def can_convert(a: str, b: str) -> bool: ``` ### Rules - Characters are compared exactly, so `"A"` and `"a"` are different characters. - The empty string is a subsequence of every string, including the empty string, so an empty `b` always gives `True`. - A non-empty `b` can never be obtained from an empty `a`. ### Constraints - `0 <= len(a), len(b) <= 10^5` - `a` and `b` consist of printable ASCII characters. ### Examples ```text Input: a = "abcde", b = "ace" Output: True ``` Removing `b` and `d` from "abcde" gives "ace". ```text Input: a = "abc", b = "acb" Output: False ``` Characters cannot be reordered. After matching `a` and then `c` (the last character of "abc"), nothing is left to match the final `b`. ```text Input: a = "xyz", b = "" Output: True ``` Removing all three characters of "xyz" leaves the empty string.

Overview: Decide whether one string can be turned into another by deleting characters without reordering the ones that remain. It tests the two-pointer subsequence scan, why matching each character as early as possible is safe, and edge cases with empty strings.

Electronic Arts logo
Electronic Arts
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two strings a and b, determine whether a can be converted into b by removing zero or more characters from a without rearranging the remaining characters.

Return True if b appears as a subsequence of a, and False otherwise.

Function Signature

def can_convert(a: str, b: str) -> bool:

Rules

  • Characters are compared exactly, so "A" and "a" are different characters.
  • The empty string is a subsequence of every string, including the empty string, so an empty b always gives True .
  • A non-empty b can never be obtained from an empty a .

Constraints

  • 0 <= len(a), len(b) <= 10^5
  • a and b consist of printable ASCII characters.

Examples

Input: a = "abcde", b = "ace"
Output: True

Removing b and d from "abcde" gives "ace".

Input: a = "abc", b = "acb"
Output: False

Characters cannot be reordered. After matching a and then c (the last character of "abc"), nothing is left to match the final b.

Input: a = "xyz", b = ""
Output: True

Removing all three characters of "xyz" leaves the empty string.