VOOZH about

URL: https://www.geeksforgeeks.org/dsa/longest-equal-substring-with-cost-less-than-k/

⇱ Longest equal substrings with cost less than K - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest equal substrings with cost less than K

Last Updated : 9 May, 2026

Given two strings s1 and s2 of the same length, which consist of lowercase letters and also an integer k. The task is to find the maximum length up to which s1 can be changed to s2 within the given cost k
The cost of changing a character is given by the absolute difference between the ASCII value of the characters. That is, to change a character at index i, cost = |s1[i] - s2[i]|

Examples:

Input: s1 = abcd, s2 = bcde, k = 3 
Output:
Explanation: The first three characters can be changed with cost = 1 + 1 + 1.  

Input: s1 = "xyzabcd", s2 = "bcdfxyw", k = 5
Output: 4
Explanation : abcd can be changed to match bcdf because the total cost to change these characters is 1 + 1 + 1 + 2 = 5, which is within k.

Input: s1 = "abcd", s2 = "cdef", k = 3
Output: 1
Explanation: A maximum of 1 character can be changed because the cost to change each character is 2, and taking more than one character would exceed the total cost K.

[Better Approach] Using Prefix Sum + Two Pointers – O(n) Time and O(n) Space

This approach uses a prefix sum array to store the cumulative cost of converting s1 to s2. Then, using a two-pointer technique, we find the longest substring whose cost does not exceed k.
The prefix array helps in calculating the cost of any substring in O(1) time.

  • Maintain two pointers, say i and j.
  • In a while loop, check if the difference between ith index and jth index of prefix array is greater than given cost or not.
  • If the difference is greater than the given cost, then increase the j pointer to compensate for the cost, else increase the i pointer.


Below is the implementation of the above approach:


Output
3

Time Complexity: O(n)
Auxiliary Space: O(n), where n is the length of the given string.

[Expected Approach] Using Sliding Window – O(n) Time and O(1) Space

This approach uses the sliding window technique to find the maximum length of a substring such that the total cost of converting s1 to s2 does not exceed k. We maintain a window with two pointers and keep track of the current cost. If the cost exceeds k, we shrink the window from the left until it becomes valid again

  1.  We maintain a sliding window of length len that starts at index 0 and ends at index i.
  2.  We calculate the total cost of changing the characters within this window and compare it with the given cost k.
  3.  If the total cost is less than or equal to k, we update the maximum length seen so far. If the total cost is greater than k, we shrink the window from the left to decrease the cost until it becomes less than or equal to k.
  4. To calculate the cost of changing the characters at each index i, we use the absolute difference between the ASCII value of the characters.
  5. Hence we return the maxlen as our Output.

Implementation:


Output
3
Comment