Advertisement
exmkg

3-5

Jan 14th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. class Solution {
  2.     public boolean backspaceCompare(String S, String T) {
  3.         return build(S).equals(build(T));
  4.     }
  5.    
  6.     private String build(String str) {
  7.         StringBuilder builtStr = new StringBuilder();
  8.         for (char c : str.toCharArray()) {
  9.             if (c != '#') { // If it's not a backspace char, append it.
  10.                 builtStr.append(c);
  11.             } else { // If it's backspace char, then..
  12.                 if (builtStr.length() != 0) {  // if the builtStr isn't empty..
  13.                     builtStr.deleteCharAt(builtStr.length() - 1); // remove last char.
  14.                 }
  15.             }
  16.         }
  17.         return builtStr.toString();
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement