|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - * 844. Backspace String Compare |
5 |
| - * |
6 |
| - * Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. |
7 |
| - * |
8 |
| - * Example 1: |
9 |
| - * Input: S = "ab#c", T = "ad#c" |
10 |
| - * Output: true |
11 |
| - * Explanation: Both S and T become "ac". |
12 |
| - * |
13 |
| - * Example 2: |
14 |
| - * Input: S = "ab##", T = "c#d#" |
15 |
| - * Output: true |
16 |
| - * Explanation: Both S and T become "". |
17 |
| - * |
18 |
| - * Example 3: |
19 |
| - * Input: S = "a##c", T = "#a#c" |
20 |
| - * Output: true |
21 |
| - * Explanation: Both S and T become "c". |
22 |
| - * |
23 |
| - * Example 4: |
24 |
| - * Input: S = "a#c", T = "b" |
25 |
| - * Output: false |
26 |
| - * Explanation: S becomes "c" while T becomes "b". |
27 |
| - * Note: |
28 |
| - * |
29 |
| - * 1 <= S.length <= 200 |
30 |
| - * 1 <= T.length <= 200 |
31 |
| - * S and T only contain lowercase letters and '#' characters. |
32 |
| - * Follow up: |
33 |
| - * |
34 |
| - * Can you solve it in O(N) time and O(1) space? |
35 |
| - */ |
36 | 3 | public class _844 {
|
37 |
| - public static class Solution1 { |
38 |
| - public boolean backspaceCompare(String S, String T) { |
39 |
| - String processedS = process(S); |
40 |
| - String processedT = process(T); |
41 |
| - return processedS.equals(processedT); |
42 |
| - } |
| 4 | + public static class Solution1 { |
| 5 | + public boolean backspaceCompare(String S, String T) { |
| 6 | + String processedS = process(S); |
| 7 | + String processedT = process(T); |
| 8 | + return processedS.equals(processedT); |
| 9 | + } |
43 | 10 |
|
44 |
| - private String process(String str) { |
45 |
| - StringBuilder sb = new StringBuilder(); |
46 |
| - for (int i = 0; i < str.length(); i++) { |
47 |
| - if (str.charAt(i) == '#') { |
48 |
| - if (sb.length() > 0) { |
49 |
| - sb.deleteCharAt(sb.length() - 1); |
50 |
| - } |
51 |
| - } else { |
52 |
| - sb.append(str.charAt(i)); |
| 11 | + private String process(String str) { |
| 12 | + StringBuilder sb = new StringBuilder(); |
| 13 | + for (int i = 0; i < str.length(); i++) { |
| 14 | + if (str.charAt(i) == '#') { |
| 15 | + if (sb.length() > 0) { |
| 16 | + sb.deleteCharAt(sb.length() - 1); |
| 17 | + } |
| 18 | + } else { |
| 19 | + sb.append(str.charAt(i)); |
| 20 | + } |
| 21 | + } |
| 22 | + return sb.reverse().toString(); |
53 | 23 | }
|
54 |
| - } |
55 |
| - return sb.reverse().toString(); |
56 | 24 | }
|
57 |
| - } |
58 | 25 | }
|
0 commit comments