-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
33 lines (32 loc) · 1.12 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
//利用StringBuilder型record操作S中的字母。如果遇到a或b则将其加入record尾部。
//遇到c则判断record最后两个字母是否为ab,如果是则将最后两个字母删除,否则将c加入。最后返回record是否为空。
using System;
using System.Text;
namespace CheckIfWordIsValidAfterSubstitutions
{
class Program
{
static void Main(string[] args)
{
string S = "abccba";
Console.WriteLine(IsValid(S));
}
static bool IsValid(string S)
{
StringBuilder record = new StringBuilder();
for (int i = 0; i < S.Length; i++)
{
if (S[i] == 'a' || S[i] == 'b')
record.Append(S[i]);
else if(S[i] == 'c')
{
if (record.Length >= 2 && record[record.Length - 1] == 'b' && record[record.Length - 2] == 'a')
record.Remove(record.Length - 2, 2);
else
record.Append(S[i]);
}
}
return record.Length == 0;
}
}
}