You are given a string s consisting of lowercase letters and an integer k.
We call a string tideal if the following conditions are satisfied:
t is a subsequence of the string s.
The absolute difference in the alphabet order of every two adjacent letters in t is less than or equal to k.
Return the length of thelongest ideal string.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
Note that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of 'a' and 'z' is 25, not 1.
Input: s ="acfgbd", k =2 Output:4 Explanation: The longest ideal string is"acbd". The length of this string is4, so 4is returned. Note that "acfgbd"is not ideal because 'c' and 'f' have a difference of 3in alphabet order.
We use dynamic programming to keep track of the longest ideal subsequence ending with each character. For each character in the string, we look at all possible previous characters within the allowed difference and update the dp state.
classSolution {
public:int longestIdealString(string s, int k) {
vector<int> dp(26, 0);
int ans =0;
for (char c : s) {
int idx = c -'a';
int best =0;
for (int j = max(0, idx - k); j <= min(25, idx + k); ++j) {
best = max(best, dp[j]);
}
dp[idx] = best +1;
ans = max(ans, dp[idx]);
}
return ans;
}
};
funclongestIdealString(sstring, kint) int {
dp:= make([]int, 26)
ans:=0for_, c:=ranges {
idx:= int(c-'a')
best:=0forj:= max(0, idx-k); j<= min(25, idx+k); j++ {
ifdp[j] > best {
best = dp[j]
}
}
dp[idx] = best+1ifdp[idx] > ans {
ans = dp[idx]
}
}
returnans}
func max(a, bint) int { ifa > b { returna }; returnb }
func min(a, bint) int { ifa < b { returna }; returnb }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
publicintlongestIdealString(String s, int k) {
int[] dp =newint[26];
int ans = 0;
for (char c : s.toCharArray()) {
int idx = c -'a';
int best = 0;
for (int j = Math.max(0, idx - k); j <= Math.min(25, idx + k); j++) {
best = Math.max(best, dp[j]);
}
dp[idx]= best + 1;
ans = Math.max(ans, dp[idx]);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funlongestIdealString(s: String, k: Int): Int {
val dp = IntArray(26)
var ans = 0for (c in s) {
val idx = c - 'a'var best = 0for (j in maxOf(0, idx - k)..minOf(25, idx + k)) {
best = maxOf(best, dp[j])
}
dp[idx] = best + 1 ans = maxOf(ans, dp[idx])
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
deflongestIdealString(self, s: str, k: int) -> int:
dp = [0] *26 ans =0for c in s:
idx = ord(c) - ord('a')
best =0for j in range(max(0, idx - k), min(25, idx + k) +1):
best = max(best, dp[j])
dp[idx] = best +1 ans = max(ans, dp[idx])
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnlongest_ideal_string(s: String, k: i32) -> i32 {
letmut dp =vec![0; 26];
letmut ans =0;
for c in s.chars() {
let idx = (c asu8-b'a') asusize;
letmut best =0;
for j in idx.saturating_sub(k asusize)..=usize::min(25, idx + k asusize) {
best = best.max(dp[j]);
}
dp[idx] = best +1;
ans = ans.max(dp[idx]);
}
ans
}
}