0

Given a string S, and two integers L, P, find the substring of length L that occur P times in the string S. For example, if S = pujanshahpujan, N = 5, P = 2, we need substrings of length 5 that occur twice in S. Clearly this is pujan.

    /* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
                Scanner sc = new Scanner(System.in);

        String str = sc.nextLine();
        int t = sc.nextInt();

        while((t--)>0) {

            int l = sc.nextInt();
            int p = sc.nextInt();
            int a = l;
            String str1 = str;

            String temp;

            for(int i=0;i<str.length();i++) {
                temp = str.substring(i, a);
                a=a+1;
                int index = str.indexOf(temp);
                int count = 0;
                while (index != -1) {
                    count++;
                    str1 = str1.substring(index + 1);
                    index = str1.indexOf(temp);
                }
                if(count==p) {
                    System.out.println(temp);
                    a=i+l+1;
                    str1=str;
                }
                }
        }
    }
}

input: mynameispujanshahandmybrothersnameischintanshah 1 4 2 output: name amei meis ansh nsha shah

0 Answers0