• 문제 링크
Backspace String Compare - LeetCode
Can you solve this real interview question? Backspace String Compare - 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 tex
leetcode.com
• 풀이 코드
public class Solution {
public boolean backspaceCompare(String s, String t) {
int i = s.length() - 1, j = t.length() - 1, a = 0, b = 0;
while (i >= 0 || j >= 0) {
while (i >= 0) {
if (s.charAt(i) == '#') {
a++;
i--;
} else if (a > 0) {
a--;
i--;
} else break;
}
while (j >= 0) {
if (t.charAt(j) == '#') {
b++;
j--;
} else if (b > 0) {
b--;
j--;
} else break;
}
if ((i >= 0 && j >= 0 && s.charAt(i) != t.charAt(j)) || i-- >= 0 != j-- >= 0) return false;
}
return true;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 860. Lemonade Change - Java (0) | 2026.10.27 |
|---|---|
| [LeetCode] 859. Buddy Strings - Java (0) | 2026.10.26 |
| [LeetCode] 836. Rectangle Overlap - Java (0) | 2026.10.24 |
| [LeetCode] 832. Flipping an Image - Java (0) | 2026.10.23 |
| [LeetCode] 830. Positions of Large Groups - Java (0) | 2026.10.23 |
댓글