Queries to check if string B exists as substring in string A |  ; Dp Array ; String to character array ; initialize matrix with 1 ; for r from 1 to string length ; for c from 1 b string length ; For every query ; print true ; print false ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; import java . io . * ; public class GFG { private static void substringCheck ( String stra , String strb , int [ ] query ) { int [ ] [ ] matrix = new int [ strb . length ( ) ] [ stra . length ( ) ] ; char [ ] charCrr = stra . toCharArray ( ) ; char [ ] charRrr = strb . toCharArray ( ) ; for ( int c = 0 ; c < stra . length ( ) ; c ++ ) { if ( charRrr [ 0 ] == charCrr ) { matrix [ 0 ] = 1 ; } } for ( int r = 1 ; r < charRrr . length ; r ++ ) { char ch = charRrr [ r ] ; for ( int c = 1 ; c < charCrr . length ; c ++ ) { if ( ch == charCrr && matrix [ r - 1 ] == 1 ) { matrix [ r ] = 1 ; } } } for ( int q : query ) { int matLoc = ( q + ( strb . length ( ) - 1 ) ) ; if ( matLoc >= stra . length ( ) ) { System . out . println ( false ) ; } else { if ( matrix [ strb . length ( ) - 1 ] [ matLoc ] == 1 ) { System . out . println ( true ) ; } else { System . out . println ( false ) ; } } } } public static void main ( String [ ] args ) { String stra = " GeeksForGeeks " ; String strb = " Geeks " ; int [ ] query = { 0 , 5 , 8 } ; substringCheck ( stra , strb , query ) ; }