oneby7

Stacks and Queues Matching and cancelling

200. Backspace String Compare

Easy

You are given two strings s and t. Each one is a list of keys pressed, in order, in an empty text editor. A lowercase letter types that letter at the end of the text. A # is the backspace key: it deletes the last letter of the text, and does nothing if the text is empty.

Return true if pressing the keys of s and pressing the keys of t leave the same text, and false otherwise. Two empty texts are the same text.

Example 1

Input: s = "plan##ot", t = "plx#ot"

Output: true

Explanation: In s, the two # delete n and then a, which leaves "pl", and "ot" makes it "plot". In t, the # deletes the x, and the text is "plot" too.

Example 2

Input: s = "tea#n", t = "ten#a"

Output: false

Explanation: Both press the same five keys. But s deletes the a and ends with "ten", while t deletes the n and ends with "tea".

Example 3

Input: s = "##x#", t = "#"

Output: true

Explanation: The first two # of s find nothing to delete, and the last one deletes the x. Both texts end empty.

Check your understanding

Work out the answer for this input before you write any code.

s = "xy##z", t = "xz"

Constraints

  • 1 ≤ s.length, t.length ≤ 105
  • s and t hold lowercase English letters and # only.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.