-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
67 lines (66 loc) · 2.64 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//创建CheckMatch方法,检查字符串p是否能通过添加小写字母与q匹配。创建pointer指针指向q首字母,创建found代表p中字母已在q中找到的个数。
//遍历p中的每个字母,用pointer指针在q中前进寻找相同的字母,找到后则令found加一,并停止遍历,并将pointer再前移一位。
//需要注意,如果pointer指针指向的为大写字母,则应在其与当前遍历到p中字母不相同的时候返回false。因为此时证明q中包含p中没有的大写字母,则一定不符合条件。
//遍历p结束后如果没有在q中找到所有字母(found小于p长度),则返回false。否则继续判断q中时候还有大写字母,如果有返回false。以上过程结束后如果仍未返回false,则返回true。
//在主方法中对每个单词调用CheckMatch方法,将结果记入res,并返回。
using System;
using System.Collections.Generic;
namespace CamelcaseMatching
{
class Program
{
static void Main(string[] args)
{
string q = "FooBar";
string p = "FoBaT";
Console.WriteLine(CheckMatch(q, p));
}
static IList<bool> CamelMatch(string[] queries, string pattern)
{
bool[] res = new bool[queries.Length];
for (int i = 0; i < queries.Length; i++)
res[i] = CheckMatch(queries[i], pattern);
return res;
}
static bool CheckMatch(string q, string p)
{
int pointer = 0;
int found = 0;
for (int i = 0; i < p.Length; i++)
{
while (pointer < q.Length)
{
if (q[pointer] >= 65 && q[pointer] <= 90)
{
if (q[pointer] == p[i])
{
found++;
break;
}
else
return false;
}
else if (q[pointer] >= 97 && q[pointer] <= 122)
{
if (q[pointer] == p[i])
{
found++;
break;
}
}
pointer++;
}
pointer++;
}
if (found < p.Length)
return false;
while (pointer < q.Length)
{
if (q[pointer] >= 65 && q[pointer] <= 90)
return false;
pointer++;
}
return true;
}
}
}