java: add «2370. Longest Ideal Subsequence»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
c45234e58f
commit
e3d66b09ab
1 changed files with 24 additions and 0 deletions
24
java/longest-ideal-subsequence.java
Normal file
24
java/longest-ideal-subsequence.java
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
class Solution {
|
||||||
|
public int longestIdealString(String s, int k) {
|
||||||
|
int[] lengths = new int[26];
|
||||||
|
|
||||||
|
int longest = 0;
|
||||||
|
for (int i = 0; i < s.length(); ++i) {
|
||||||
|
int c = s.charAt(i) - 'a';
|
||||||
|
|
||||||
|
int longestIncluding = 0;
|
||||||
|
for (int pred = 0; pred < 26; ++pred) {
|
||||||
|
if (Math.abs(pred - c) > k) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
longestIncluding = Math.max(longestIncluding, lengths[pred]);
|
||||||
|
}
|
||||||
|
|
||||||
|
lengths[c] = Math.max(lengths[c], longestIncluding + 1);
|
||||||
|
longest = Math.max(longest, lengths[c]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return longest;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue