Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean backspaceCompare(String S, String T) {
- return build(S).equals(build(T));
- }
- private String build(String str) {
- StringBuilder builtStr = new StringBuilder();
- for (char c : str.toCharArray()) {
- if (c != '#') { // If it's not a backspace char, append it.
- builtStr.append(c);
- } else { // If it's backspace char, then..
- if (builtStr.length() != 0) { // if the builtStr isn't empty..
- builtStr.deleteCharAt(builtStr.length() - 1); // remove last char.
- }
- }
- }
- return builtStr.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement