Substring Diff


Problem Statement :


In this problem, we'll use the term "longest common substring" loosely. It refers to substrings differing at some number or fewer characters when compared index by index. For example, 'abc' and 'adc' differ in one position, 'aab' and 'aba' differ in two.

Given two strings and an integer k, determine the length of the longest common substrings of the two strings that differ in no more than k positions.

For example, k=1. Strings s1=abcd and s2=bbca. Check to see if the whole string (the longest substrings) matches. Given that neither the first nor last characters match and 2>k, we need to try shorter substrings. The next longest substrings are s1' = [abc, bcd] and s2' = [bbc, bca]. Two pairs of these substrings only differ in 1 position: [abc, bbc] and [bcd, bca]. They are of length 3.

Function Description

Complete the substringDiff function in the editor below. It should return an integer that represents the length of the longest common substring as defined.

substringDiff has the following parameter(s):

k: an integer that represents the maximum number of differing characters in a matching pair
s1: the first string
s2: the second string

Input Format

The first line of input contains a single integer, t, the number of test cases follow.
Each of the next t lines contains three space-separated values: an integer k and two strings, s1 and s2.

Constraints

1 <= t <= 10
0 <= k <= |s1|
|s1|  = |s2|
1 <= |s1|, |s2| <= 1500
All characters in s1 and s2 ∈ ascii[a-z].
Output Format

For each test case, output a single integer which is the length of the maximum length common substrings differing at k or fewer positions.



Solution :



title-img


                            Solution in C :

In C++ :






#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <bitset>
#include <cmath>
#include <complex>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <stdlib.h>
#include <utility>
#include <ctime>
using namespace std;

#define MOD 1000000007
#define BIT(x) __builtin_popcount(x)

int n , k;
int D[1505][1505],K[1505][1505];
char A[1505],B[1505];

int main()
{
	int t; cin >> t;
while(t--){
  scanf("%d",&k);
  scanf("%s%s",A,B);
  n = strlen(A);
  int r = 0;
  memset(D,0,sizeof(D));
  memset(K,0,sizeof(K));
  for(int i = n-1; i>=0 ; i--)
	for(int j = n-1; j>=0 ; j--){
     D[i][j] = D[i+1][j+1] + 1;
     K[i][j] = K[i+1][j+1] + ((A[i]==B[j])?0:1);
     while(K[i][j]>k){
    	 if(A[i+D[i][j]-1] != B[j+D[i][j]-1])
    		 K[i][j]--;
    	 D[i][j]--;
     }
     r = max(r, D[i][j]);
	}
  cout << r << endl;
}
  return 0;
}








In Java :






import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;

public class Solution {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    public static void main(String[] args) {
        new Solution().run();
    }

    public void run() {
        try {
            long t1 = System.currentTimeMillis();
               in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
           
            Locale.setDefault(Locale.US);
            solve();
            in.close();
            out.close();
            long t2 = System.currentTimeMillis();
            System.err.println("Time = " + (t2 - t1));
        } catch (Throwable t) {
            t.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    // solution
    void solve() throws IOException {
        int n = readInt();
        for (int i = 0; i < n; i++)
        {
            int k = readInt();
            String s1 = readString();
            String s2 = readString();
            out.println(Math.max(find(k, s1, s2), find(k, s2, s1)));
        }
    }
    
    int find(int k, String s1, String s2)
    {
        int max = 0;
        for (int startFrom = 0; startFrom < s1.length(); startFrom++)
        {
            int l = 0;
            int penalty = 0;
            for (int r = 0; (r < s2.length()) && (startFrom + r < s1.length()); r++)
            {
                if (s1.charAt(startFrom + r) != s2.charAt(r))
                    penalty++;
                while (penalty > k)
                {
                   if (s1.charAt(startFrom + l) != s2.charAt(l))
                       penalty--;
                   l++;
                }
                max = Math.max(max, r - l + 1);
            }
        }
        return max;
    }
}








In C :





#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1500

char buf[4096];
char diff[MAXN][MAXN];
int n, k;

void mkdiff(char *s, char *t) {
  int i, j;

  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      diff[i][j] = (s[i] == t[j]) ? 0 : 1;
}

int isgood(int L) {
  int d, i, j, sum;

  if (L <= k)
    return 1;

  for (d = -n+1; d <= n-1; d++) {
    if (d <= 0) {
      if (n + d < L)
	continue;
      sum = 0;
      for (i = -d, j = 0; i < n; i++, j++) {
	sum += diff[i][j];
	if (j >= L)
	  sum -= diff[i-L][j-L];
	if (j >= L-1 && sum <= k)
	  return 1;
      }
    } else {
      if (n - d < L)
	continue;
      sum = 0;
      for (i = 0, j = d; j < n; i++, j++) {
	sum += diff[i][j];
	if (i >= L)
	  sum -= diff[i-L][j-L];
	if (i >= L-1 && sum <= k)
	  return 1;
      }
    }
  }
  return 0;
}

/* binary search to find largest L which satisfies M(i,j,L) <= k */
int search() {
  int i, j, m;

  // Invariant: f(i-1)=true, f(j)=false
  i = 0;
  j = n+1;
  while (i < j) {
    m = (i + j) / 2;
    if (isgood(m))
      i = m+1;
    else
      j = m;
  }
  return i-1;
}

int main() {
  int ncases;
  char *s, *t;

  fgets(buf, sizeof buf, stdin);
  ncases = atoi(buf);

  while (ncases-- > 0) {
    fgets(buf, sizeof buf, stdin);
    s = strchr(buf, ' ');
    *s++ = '\0';
    t = strchr(s, ' ');
    *t++ = '\0';
    k = atoi(buf);
    n = strlen(s);
    if (t[n] == '\n')
      t[n] = '\0';

    mkdiff(s, t);

    printf("%d\n", search(0, n));
  }

  return 0;
}








In Python3 :





def l_func(p,q,max_s):
    n = len(q)
    res_ar = [0]
    count = 0
    ans = 0
    for i in range(n):
        if(p[i]!=q[i]):
            res_ar.append(i+1)
            count += 1
            if(count>max_s):
                l = res_ar[-1]-res_ar[-2-max_s]-1
                if(l>ans):ans = l
    if(count<=max_s):
        return n
    return ans

def check_func(p,q,s):
    n = len(q)
    ans = 0
    for i in range(n):
        if(n-i<=ans):break
        l = l_func(p,q[i:],s)
        if(l>ans):
            ans = l
    for i in range(n):
        if(n-i<=ans):break
        l = l_func(q,p[i:],s)
        if(l>ans):
            ans = l
    return ans
for case_t in range(int(input())):
    str_s,p,q = input().strip().split()
    s = int(str_s)
    print(check_func(p,q,s))
                        








View More Similar Problems

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →