844. Backspace String Compare

Backspace String Compare (LeetCode 844) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Two Pointers pattern, in the String Comparison with Backspaces subpattern.

How to solve Backspace String Compare

Backspace String Compare is a String Comparison with Backspace Characters problem, part of Two Pointers. Learn to compare strings with backspace characters using two pointers from the end.

Work towards the solution in these steps:

  1. Hint 1: Simulate the typing process for each string, handling '#' as a backspace.
  2. Hint 2: You can use a stack to build the final string for both s and t, then compare the results.
  3. Hint 3: For O(1) space, use two pointers starting from the end of each string, skipping characters as needed when encountering '#'.

The reference solution runs in O(N + M) time and O(1) space.

The Backspace String Compare solution and editorial walks through the full approach with reference code.

Problem statement

Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

Example 1:

Input: s = "ab#c", t = "ad#c" Output: true Explanation: Both s and t become "ac".

Example 2:

Input: s = "ab##", t = "c#d#" Output: true Explanation: Both s and t become "".

Example 3:

Input: s = "a#c", t = "b" Output: false Explanation: s becomes "c" while t becomes "b".

Constraints:

- 1 <= s.length, t.length <= 200

- s and t only contain lowercase letters and '#' characters.

Follow up: Can you solve it in O(n) time and O(1) space?

Topics and companies

Topics: Two Pointers, String, Stack, Simulation.

Reported in interviews at Facebook, Google, Amazon, Oracle.

How to practise Backspace String Compare on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N + M) time and O(1) space.

Open Backspace String Compare in the code editor, or read the Backspace String Compare editorial for a worked solution with its approach and complexity analysis.

Where Backspace String Compare sits in the DSA pattern sheet

Problems related to Backspace String Compare

Other problems that use the same String Comparison with Backspaces and Two Pointers techniques:

Backspace String Compare FAQ

What is the LeetCode number of Backspace String Compare?

Backspace String Compare is LeetCode problem 844, rated Easy.

What pattern is used to solve Backspace String Compare?

Backspace String Compare is a String Comparison with Backspace Characters problem. It belongs to the Two Pointers pattern, in the String Comparison with Backspaces subpattern.

What is the time complexity of Backspace String Compare?

The reference solution runs in O(N + M) time and O(1) space.

Which companies ask Backspace String Compare?

Backspace String Compare has been reported in interviews at Facebook, Google, Amazon, Oracle.

Preparing this workspace.