Longest Common Extension / LCE | Set 1 ( Introduction and Naive Method ) | A Java Program to find the length of longest common extension using Naive Method ; Structure to represent a query of form ( L , R ) ; A utility function to find longest common extension from index - L and index - R ; A function to answer queries of longest common extension ; Driver code ; LCA Queries to answer
import java . util . * ; class GFG { static class Query { int L , R ; Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static int LCE ( String str , int n , int L , int R ) { int length = 0 ; while ( R + length < n && str . charAt ( L + length ) == str . charAt ( R + length ) ) length ++ ; return ( length ) ; } static void LCEQueries ( String str , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L ; int R = q [ i ] . R ; System . out . printf ( "LCE (%d, %d) = %dNEW_LINE", L, R, LCE ( str , n , L , R ) ) ; } return ; } public static void main ( String [ ] args ) { String str = " abbababba " ; int n = str . length ( ) ; Query q [ ] = new Query [ 3 ] ; q [ 0 ] = new Query ( 1 , 2 ) ; q [ 1 ] = new Query ( 1 , 6 ) ; q [ 2 ] = new Query ( 0 , 5 ) ; int m = q . length ; LCEQueries ( str , n , q , m ) ; } }