-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackspaceStringCompare.java
More file actions
35 lines (26 loc) · 1.07 KB
/
BackspaceStringCompare.java
File metadata and controls
35 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Given two strings S and T, return if they are equal when both are typed into empty text editors.
// # means a backspace character.
// See: https://leetcode.com/problems/backspace-string-compare/
package leetcode.stack;
import java.util.Stack;
public class BackspaceStringCompare {
public boolean backspaceCompare(String S, String T) {
return reduce(S).equals(reduce(T));
}
private Stack<Character> reduce(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) != '#')
stack.add(s.charAt(i));
else if (!stack.empty())
stack.pop();
return stack;
}
public static void main(String[] args) {
BackspaceStringCompare sln = new BackspaceStringCompare();
System.out.println(sln.backspaceCompare("ab#c", "ad#c"));
System.out.println(sln.backspaceCompare("ab##", "c#d#"));
System.out.println(sln.backspaceCompare("ab##", "a#c"));
System.out.println(sln.backspaceCompare("##c", "#c"));
}
}