/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String s1 = "bca";
		int k1 = 1;
		System.out.println("Test Case 1 Max Length: " + longestValidSubstring(s1, k1));
	}

	public static int longestValidSubstring(String s, int k) {
		int n = s.length();
		if (n == 0) return 0;

		// dp[i] stores the length of the longest valid substring ending at index i
		int[] dp = new int[n];
		
		// Base case: a single character is always a valid substring of length 1
		dp[0] = 1;
		int maxLength = 1;

		
		for (int i = 1; i < n; i++) {
			if (Math.abs(s.charAt(i) - s.charAt(i - 1)) <= k) {
				dp[i] = dp[i - 1] + 1;
			} else {
				dp[i] = 1;
			}
			
			maxLength = Math.max(maxLength, dp[i]);
		}

		return maxLength;
	}
}