-
Notifications
You must be signed in to change notification settings - Fork 1
/
BackspaceStringCompare.java
47 lines (40 loc) · 1.12 KB
/
BackspaceStringCompare.java
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
36
37
38
39
40
41
42
43
44
45
46
47
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.Stack;
// https://leetcode.com/problems/backspace-string-compare/
public class BackspaceStringCompare {
private final String s;
private final String t;
public BackspaceStringCompare(String s, String t) {
this.s = s;
this.t = t;
}
public boolean solution() {
Stack<Character> f = printChars(s);
Stack<Character> n = printChars(t);
if (f.size() != n.size()) {
return false;
}
for (int i = 0; i < f.size(); i++) {
char fC = f.get(i);
char nC = n.get(i);
if (fC != nC) {
return false;
}
}
return true;
}
private Stack<Character> printChars(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '#') {
if (!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(c);
}
}
return stack;
}
}