Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:03:41+00:00 2026-05-31T04:03:41+00:00

Given two strings of length n,P = p1…pn and Q = q1…qn, we define

  • 0

Given two strings of length n,P = p1…pn and Q = q1…qn, we define M(i, j, k) as the number of mismatches between pi…pi+k-1 and qj..qj+k-1. That is in set notation, M(i, j, k) refers to the size of the set { 0<=x<k | pi+x not equal to qj+x| }.

Given an integer K, your task is to find the maximum length L such that there exists pair of indices (i,j) for which we have M(i, j, L) <= K. Of course, we should also have i+L-1 <=n and j+L-1 <=n.
Input

First line of input contains a single integer T (1 <=T <=10). T test cases follow.
Each test case consists of an integer K and two strings P and Q separated by a single space.
Output

For each test case output a single integer L which is the maximum value for which there exists pair of indices (i,j) such that M(i, j, L) <=K.

Constraints

0 <= K <= length of the string P
Both P & Q would have the same length
The size of each of the string would be at the max 1500
All characters in P & Q are lower-case English letters.

Sample Input

3
2 tabriz torino
0 abacba abcaba
3 helloworld yellomarin

Sample Output

4
3
8 

Explanation:
First test-case: If we take “briz” from the first string, and “orin” from the second string, then the number of mismatches between these two substrings is equal to 2, and the length of these substrings are 4. That’s we have chosen i=3, j=2, L=4, and we have M(3,2,4) = 2.

Second test-case: Since K=0, we should find the longest common substring for the given input strings. We can choose “aba” as the result, and we don’t have longer common substring between two strings. So, the answer is 3 for this test-case. That’s we have chosen i=1, j=4, and L=3, and we have M(1,4,3)=0.

Third test-case: We can choose “hellowor” from first string and “yellomar” from the second string. So, we have chosen i=1, j=1, and L=8, and we have M(1,1,8)=3. Of course we can also choose i=2, j=2, and L=8 and we still have M(2,2,8)=3.

here is my implementation

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

class Solution {

    public static int mismatch(String a, String b, int ii, int jj, int xx) {
        int i, j = 0;
        for (i = 0; i < xx; i++) {
            if (a.charAt(ii) != b.charAt(jj)) {
                j++;
            }
            ii++;
            jj++;
        }
        return j;
    }

    public static boolean find(int x, String a, String b, int kx) {
        int nn = a.length();
        for (int i = 0; i <= (nn - x); i++) {
            for (int j = 0; j <= (nn - x); j++) {
                int k;
                k = mismatch(a, b, i, j, x);
                if (k == kx) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String args[]) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t > 0) {
            int k, n;
            String a, b;
            k = scanner.nextInt();
            a = scanner.next();
            b = scanner.next();
            n = a.length();
            int i = (n + k) / 2;
            int st = k, en = n
                while (i != k || i != n) {
                boolean ch = false, chh = false;
                ch = find(i, a, b, k);
                if (i != n) {
                    chh = find(i + 1, a, b, k);
                }
                if (i == n && ch == true) {
                    System.out.println(i);
                    break;
                }
                if (ch == true && chh == false) {
                    System.out.println(i);
                    break;
                }
                if (ch) {
                    st = i;
                    i = (i + en + 1) / 2;
                } else {
                    en = i;
                    i = (st + i) / 2;
                }
            }
            t--;
        }
    }
}

the above implementation is taking 5.1 sec for input 0f 1500 string length.But maximum time limit in java is 5sec.if any one can improve this code,please kindly share yor thougths

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:03:43+00:00Added an answer on May 31, 2026 at 4:03 am

    Your code doesn’t take 5.1s on the site. They stop running your code as soon as it exceeds the time limit. Your code might be taking even minutes. So, even if you optimize it with this algorithm you will again get 5.1s in details section. So work on your algo, not optimization!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not homework. Given two strings S and T of same length. Given a set
Given two strings of equal length such that s1 = ACCT s2 = ATCT
Given two strings, I want to find all common substrings of a specified length,
I want to implement a check function that given two strings s1 and s2
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {
Possible Duplicate: substring algorithm Given two strings, A and B, how to find the
Given the following strings 7;#User One 7;#User Two;#9;#User Two 7;#User Two;#9;#User Two;#123;#User Three I
I'm attempting to write a basic dna sequencer. In that, given two sequences of
I'm looking to assess similarity (including case) between two strings and give a value
I have a function that is given two integers and returns a string. Right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.