Compare Two Backspace-Edited Strings

Quick Overview

Compare Two Backspace-Edited Strings. Type each string into a separate empty editor. Lowercase letters append themselves and # deletes the latest remaining character, doing nothing when the editor is empty.

Compare Two Backspace-Edited Strings

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Type each string into a separate empty editor. Lowercase letters append themselves and # deletes the latest remaining character, doing nothing when the editor is empty. Return whether the two final texts are equal. ```cpp class Solution { public: bool backspaceCompare(string s, string t); }; ``` ### Examples **Example 1:** ```text s = "xy#z", t = "xz" Output: true Explanation: Backspacing y leaves xz in both editors. ``` **Example 2:** ```text s = "##a", t = "#b" Output: false Explanation: Extra leading backspaces leave final texts a and b. ``` ### Constraints - Each input has 1 through 200 characters. - Only lowercase English letters and # occur. ### Contract considerations Handle backspacing an empty editor without inventing negative text length. ```hint Read the boundary condition How many earlier letters are erased by the backspaces seen to the right? ``` ### Boundary reminders Handle backspacing an empty editor without inventing negative text length. ### Optional extensions - Can you compare the final texts in linear time using constant extra storage?

Overview: Compare Two Backspace-Edited Strings. Type each string into a separate empty editor. Lowercase letters append themselves and # deletes the latest remaining character, doing nothing when the editor is empty.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Type each string into a separate empty editor. Lowercase letters append themselves and # deletes the latest remaining character, doing nothing when the editor is empty. Return whether the two final texts are equal.

class Solution {
public:
    bool backspaceCompare(string s, string t);
};

Examples

Example 1:

s = "xy#z", t = "xz"
Output: true
Explanation: Backspacing y leaves xz in both editors.

Example 2:

s = "##a", t = "#b"
Output: false
Explanation: Extra leading backspaces leave final texts a and b.

Constraints

  • Each input has 1 through 200 characters.
  • Only lowercase English letters and # occur.

Contract considerations

Handle backspacing an empty editor without inventing negative text length.

Boundary reminders

Handle backspacing an empty editor without inventing negative text length.

Optional extensions

  • Can you compare the final texts in linear time using constant extra storage?