Longest special subsequences
Practice
2.7 (13 votes)
Algorithms
Dynamic programming
Java
Medium
Optimization
Python
Two dimensional
Problem
53% Success 1859 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a string S of the length n consisting of only lowercase English alphabets.

If the two consecutive characters in a subsequence have a difference that is no more than k, then it is called a special subsequence. Find the length of the longest special subsequence.

Input format

  •  First line: t (number of test cases) 
  •  Next t line: Three space-separated integers n, k, and S 

Output format

For each test case, print the length of the longest special subsequence in a new line.

Constraints

\(1 \le t \le 10\)

\(1 \le n \le 10^{5}\)

\(1 \le k \le 26\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
AlgorithmsDynamic Programming2D dynamic programming
Points:30
14 votes
Tags:
ApprovedCombinatoricsDynamic ProgrammingMathMediumOpenTwo dimensional
Points:30
15 votes
Tags:
2D dynamic programmingAlgorithmsData StructuresDynamic Programming