-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0408-ValidWordAbbreviation.cs
40 lines (37 loc) · 1.21 KB
/
0408-ValidWordAbbreviation.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
//-----------------------------------------------------------------------------
// Runtime: 72ms
// Memory Usage: 23.4 MB
// Link: https://leetcode.com/submissions/detail/359604383/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0408_ValidWordAbbreviation
{
public bool ValidWordAbbreviation(string word, string abbr)
{
int i = 0, j = 0;
while (i < word.Length && j < abbr.Length)
{
if (abbr[j] >= 'a' && abbr[j] <= 'z')
{
if (word[i] != abbr[j]) return false;
i++;
j++;
}
else
{
var count = 0;
while (j < abbr.Length && abbr[j] >= '0' && abbr[j] <= '9')
{
if (abbr[j] == '0' && count == 0) return false;
count *= 10;
count += abbr[j] - '0';
j++;
}
i += count;
}
}
return i == word.Length && j == abbr.Length;
}
}
}