-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
45 lines (44 loc) · 1.26 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
//双指针,移动hi扩大包含字母的范围。当包含所有字母的时候将其及其之后的左右可能加入res。
//之后收缩li寻找所有依然很符合条件的区间。
using System;
using System.Linq;
namespace NumberofSubstrings
{
class Program
{
static void Main(string[] args)
{
string s = "abcabc";
Console.WriteLine(NumberOfSubstrings(s));
}
static int NumberOfSubstrings(string s)
{
int[] record = new int[3];
int res = 0;
int li = 0, hi = 0;
while (li < s.Length)
{
while (hi < s.Length)
{
record[s[hi] - 'a']++;
if (record.All(r => r >= 1))
{
res += s.Length - hi;
hi++;
break;
}
hi++;
}
record[s[li] - 'a']--;
li++;
while (record.All(r => r >= 1))
{
res += s.Length - hi + 1;
record[s[li] - 'a']--;
li++;
}
}
return res;
}
}
}