-
Notifications
You must be signed in to change notification settings - Fork 115
/
0844-BackspaceStringCompare.cs
49 lines (45 loc) · 1.34 KB
/
0844-BackspaceStringCompare.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
//-----------------------------------------------------------------------------
// Runtime: 64ms
// Memory Usage: 22.3 MB
// Link: https://leetcode.com/submissions/detail/322438313/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0844_BackspaceStringCompare
{
public bool BackspaceCompare(string S, string T)
{
var i = S.Length - 1;
var j = T.Length - 1;
var skipS = 0;
var skipT = 0;
while (i >= 0 || j >= 0)
{
while (i >= 0)
{
if (S[i] == '#')
skipS++;
else if (skipS > 0)
skipS--;
else
break;
i--;
}
while (j >= 0)
{
if (T[j] == '#')
skipT++;
else if (skipT > 0)
skipT--;
else
break;
j--;
}
if (i >= 0 && j >= 0 && S[i] != T[j]) return false;
if ((i >= 0) != (j >= 0)) return false;
i--; j--;
}
return true;
}
}
}