Find the longest common prefix between two strings after performing swaps on second string | Java program to find the longest common prefix between two strings after performing swaps on the second string ; int a = x . length ( ) ; length of x int b = y . length ( ) ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; Driver Code
public class GFG { static void LengthLCP ( String x , String y ) { int fr [ ] = new int [ 26 ] ; for ( int i = 0 ; i < b ; i ++ ) { fr [ y . charAt ( i ) - 97 ] += 1 ; } int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { if ( fr [ x . charAt ( i ) - 97 ] > 0 ) { c += 1 ; fr [ x . charAt ( i ) - 97 ] -= 1 ; } else break ; } System . out . println ( ( c ) ) ; } public static void main ( String args [ ] ) { String x = " here " , y = " there " ; LengthLCP ( x , y ) ; } }