-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path844.BackspaceCompare.cs
131 lines (109 loc) · 3.2 KB
/
844.BackspaceCompare.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 844. 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 text will continue empty.
// Example 1:
// Input: s = "ab#c", t = "ad#c"
// Output: true
// Explanation: Both s and t become "ac".
// Example 2:
// Input: s = "ab##", t = "c#d#"
// Output: true
// Explanation: Both s and t become "".
// Example 3:
// Input: s = "a#c", t = "b"
// Output: false
// Explanation: s becomes "c" while t becomes "b".
// Constraints:
// 1 <= s.length, t.length <= 200
// s and t only contain lowercase letters and '#' characters.
public class Solution {
public bool BackspaceCompare(string s, string t) {
List<char> slist = new();
List<char> tlist = new();
foreach(char c in s) {
if(c == '#') {
if(slist.Count > 0)
slist.RemoveAt(slist.Count - 1);
} else {
slist.Add(c);
}
}
foreach(char c in t) {
if(c == '#') {
if(tlist.Count > 0)
tlist.RemoveAt(tlist.Count - 1);
} else {
tlist.Add(c);
}
}
return new string(slist.ToArray()) == new string(tlist.ToArray());
}
}
public class Solution {
public bool BackspaceCompare(string s, string t) {
Stack<char> sStack = new Stack<char>();
Stack<char> tStack = new Stack<char>();
foreach (char c in s) {
if (c == '#') {
if (sStack.Count > 0) {
sStack.Pop();
}
} else {
sStack.Push(c);
}
}
foreach (char c in t) {
if (c == '#') {
if (tStack.Count > 0) {
tStack.Pop();
}
} else {
tStack.Push(c);
}
}
return sStack.SequenceEqual(tStack);
}
}
public class Solution {
public bool BackspaceCompare(string s, string t) {
int i = s.Length - 1, j = t.Length - 1;
int skipS = 0, skipT = 0;
while (i >= 0 || j >= 0) {
// Process backspaces in s
while (i >= 0) {
if (s[i] == '#') {
skipS++;
i--;
} else if (skipS > 0) {
skipS--;
i--;
} else {
break;
}
}
// Process backspaces in t
while (j >= 0) {
if (t[j] == '#') {
skipT++;
j--;
} else if (skipT > 0) {
skipT--;
j--;
} else {
break;
}
}
// Compare characters
if (i >= 0 && j >= 0 && s[i] != t[j]) {
return false;
}
// If one string is exhausted
if ((i >= 0) != (j >= 0)) {
return false;
}
i--;
j--;
}
return true;
}
}