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

Powered by Blogger.

Saturday, April 28, 2018

Check for subsequence


Given two strings a and b, find if a is a sub sequence of b.

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class isSubstring
 {
   public static boolean isSubstring(String pattern,String str)
   {
     String ptrn=new String();
     for(int i=0;i<pattern.length()-1;++i)
     {
       ptrn+=String.valueOf(pattern.charAt(i)+".*");
     }
     ptrn+=String.valueOf(pattern.charAt(pattern.length()-1));
    // System.out.println(ptrn);
     Pattern p=Pattern.compile(ptrn);
     Matcher m=p.matcher(str);
     return m.find();
   }
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
    System.out.println(isSubstring(ab.next(),ab.next())?"1":"0");
}
}
}

0 Comments:

Post a Comment

Stats