BackhardStringsGoldman SachsCred

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.

Example 1
Input
abc
Output
2

Explanation: Step-by-step: To make 'abc' a palindrome, we need to delete 'c' and then insert 'c' at the beginning to get 'cba'. This requires 2 edits.

Example 2
Input
abcd
Output
3

Explanation: Step-by-step: To make 'abcd' a palindrome, we need to delete 'd' and then insert 'd' at the beginning to get 'dbca'. This requires 3 edits.

Constraints

  • 1 ≤ k ≤ 100
  • 1 ≤ length of s ≤ 1000
  • s contains only lowercase alphabets
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Edits for Palindromic Substrings — Problem Statement & Solution Guide

StringsHardMixed
TimeO(n*k)
|
SpaceO(1)

Problem Description

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

abc

Output

2

Explanation: Step-by-step: To make 'abc' a palindrome, we need to delete 'c' and then insert 'c' at the beginning to get 'cba'. This requires 2 edits.

Example 2

Input

abcd

Output

3

Explanation: Step-by-step: To make 'abcd' a palindrome, we need to delete 'd' and then insert 'd' at the beginning to get 'dbca'. This requires 3 edits.

Constraints

  • 1 ≤ k ≤ 100
  • 1 ≤ length of s ≤ 1000
  • s contains only lowercase alphabets

Optimal Approach & Strategy

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.

Brute Force Approach

A naive approach would involve iterating over the string to generate all substrings of length k and then for each substring, determining the minimum edits required to make it a palindrome, resulting in a high time complexity.

Verified Code Solutions

JavaScript Solution
Time: O(n*k)
function minEdits(s, k) {
   let n = s.length;
   let minEdits = 0;
   for (let i = 0; i <= n - k; i++) {
       let substr = s.substring(i, i + k);
       let left = 0;
       let right = k - 1;
       while (left < right) {
           if (substr[left] !== substr[right]) {
               minEdits++;
           }
           left++;
           right--;
       }
   }
   return minEdits;
}

Asked in Top Tech Interviews

Goldman SachsCred

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.