hardStringsPattern: Mixed

Minimum Edits for Palindromic Substrings Solution

Problem Statement

Given a string s and an integer k, determine the minimum number of edits (insertions or deletions) required to transform all substrings of length k in s into palindromes. An edit is defined as either inserting or deleting a single character from the substring.

Examples

Example 1:
Input:s = 'abcba', k = 3
Output:2
Explanation: Substrings of length 3 are 'abc', 'bcb', 'cba'. 'abc' and 'bcb' are not palindromes, each requiring 1 edit to become 'aba' and 'bab', respectively. However, 'cba' is already a palindrome. Thus, the minimum edits required is 2.
Example 2:
Input:s = 'aabba', k = 4
Output:1
Explanation: Substrings of length 4 are 'aabb', 'abba'. 'aabb' is not a palindrome and requires 1 edit to become 'abba', which is a palindrome. 'abba' is already a palindrome. Thus, the minimum edits required is 1.

Constraints

  • 1 ≤ k ≤ 100
  • 1 ≤ length of s ≤ 1000
  • s contains only lowercase alphabets
Time: O(n*k) Space: O(1)
The optimal approach involves using a two-pointer technique to compare characters in each substring from both ends towards the center, counting the differences, and keeping track of the total minimum edits needed across all substrings, achieving a lower time complexity.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.