Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, August 6, 2016

How Many String Hackerrank


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static List<String> lst=new ArrayList<String>();
    public static void substr(String str,int n,int time)
    {
         if(time==str.length())
            return ;
        //if(i==n-time)
           for(int i=0;i<=n-time;i++)
           {
        if(!lst.contains(str.substring(i,i+time)))
        lst.add(str.substring(i,i+time));
           }
            substr(str,n,time+1);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int q = in.nextInt();
        String s = in.next();
        for(int a0 = 0; a0 < q; a0++){
            int left = in.nextInt();
            int right = in.nextInt();
            lst.clear();
            substr(s.substring(left,right+1),right-left+1,0);
          /*  for(String f:lst)
                System.out.print(f+" "); */
            System.out.println(lst.size());
        }
    }
}

0 Comments:

Post a Comment

Stats