FastPrepBackspace String Compare

Backspace String Compare

Goldman Sachs logoGoldman Sachs● EasyNEW GRADOA

Problem statement

You are given two strings s1 and s2 containing lowercase English letters and # characters. A # acts as a backspace and removes the preceding remaining character. A backspace applied to an empty string has no effect.

Process both strings and return 1 if their final contents are equal; otherwise return 0.

Function

compareStrings(s1: String, s2: String) → int

Examples

Example 1

s1 = "axx#b#b#c"s2 = "axbdf#c#c"return = 0

After applying backspaces, axx#b#b#c becomes axc, while axbdf#c#c becomes axbdc. The final strings differ, so the answer is 0.

Constraints

  • 1 <= s1.length <= 2 * 10^5
  • 1 <= s2.length <= 2 * 10^5
  • Both strings contain only lowercase English letters and #.

More Goldman Sachs problems

See Goldman Sachs hiring insights
Saved
public int compareStrings(String s1, String s2) {
  // write your code here
}
s1"axx#b#b#c"
s2"axbdf#c#c"
expected0
Checking account…