-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
44 lines (43 loc) · 1.38 KB
/
Program.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
//创建两个栈存储字符。
//分别遍历字符串,在栈不为空,且当前字符为#时,进行弹栈。在当前字符不为#时,将其压入栈中。
//分别获取结果字符串,比较其是否相等。
using System;
using System.Collections;
namespace BackspaceStringCompare
{
class Program
{
static void Main(string[] args)
{
string S = "y#fo##f";
string T = "y#f#o##f";
Console.WriteLine(BackspaceCompare(S, T));
}
static bool BackspaceCompare(string S, string T)
{
Stack sStack = new Stack();
Stack tStack = new Stack();
for (int i = 0; i < S.Length; i++)
{
if (sStack.Count != 0 && S[i] == '#')
sStack.Pop();
else if(S[i] != '#')
sStack.Push(S[i]);
}
for (int i = 0; i < T.Length; i++)
{
if (tStack.Count != 0 && T[i] == '#')
tStack.Pop();
else if (T[i] != '#')
tStack.Push(T[i]);
}
string sRes = "";
string tRes = "";
while (sStack.Count != 0)
sRes = sStack.Pop() + sRes;
while (tStack.Count != 0)
tRes = tStack.Pop() + tRes;
return sRes == tRes;
}
}
}