Skip to content

Commit e502471

Browse files
authored
Create 2370. Longest Ideal Subsequence (#465)
2 parents f2dc01f + 17c8f83 commit e502471

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

2370. Longest Ideal Subsequence

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
4+
int solve(string&s, int k){
5+
6+
vector<int>tab(26);
7+
8+
int i, j, maxi = 0;
9+
10+
for(i = s.length()-1; i>-1; i--){
11+
for(j = 0; j<=25; j++){
12+
if(abs(s[i]-(j+97))<=k && tab[j]){
13+
tab[s[i]-97] = max(tab[s[i]-97], tab[j]);
14+
}
15+
}
16+
tab[s[i]-97]++;
17+
maxi = max(maxi, tab[s[i]-97]);
18+
}
19+
return maxi;
20+
}
21+
22+
int longestIdealString(string s, int k) {
23+
return solve(s, k);
24+
}
25+
};

0 commit comments

Comments
 (0)