Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b899e7

Browse files
Create 3458.Select-K-Disjoint-Special-Substrings_v2.cpp
1 parent 37c8c82 commit 3b899e7

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
public:
3+
bool maxSubstringLength(string s, int k)
4+
{
5+
int n = s.size();
6+
vector<vector<int>>pos(26);
7+
for (int i=0; i<n; i++)
8+
pos[s[i]-'a'].push_back(i);
9+
10+
vector<pair<int, int>>intervals;
11+
for (int letter=0; letter<26; letter++)
12+
{
13+
if (pos[letter].empty()) continue;
14+
int start = pos[letter][0];
15+
int i = start;
16+
int far = pos[letter].back();
17+
18+
bool flag = true;
19+
while (i<=far)
20+
{
21+
far = max(far, pos[s[i]-'a'].back());
22+
if (pos[s[i]-'a'][0]<start)
23+
{
24+
flag = false;
25+
break;
26+
}
27+
i++;
28+
}
29+
if (far-start+1==n) continue;
30+
if (flag==false) continue;
31+
intervals.push_back({start, far});
32+
}
33+
34+
return helper(intervals)>=k;
35+
}
36+
37+
bool contains(pair<int,int>a, pair<int,int>b)
38+
{
39+
return a.first<b.first && a.second>b.second;
40+
}
41+
42+
int helper(vector<pair<int, int>> &intervals) {
43+
int n = intervals.size();
44+
vector<int>check(n, 1);
45+
for (int i=0; i<n; i++)
46+
for (int j=0; j<n; j++)
47+
{
48+
if (i==j) continue;
49+
if (contains(intervals[i],intervals[j]))
50+
check[i] = 0;
51+
if (contains(intervals[j],intervals[i]))
52+
check[j] = 0;
53+
}
54+
int ret = 0;
55+
for (int i=0; i<n; i++) ret += check[i];
56+
return ret;
57+
}
58+
59+
60+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /