Longest string which is prefix string of at least two strings | C # program to find longest String which is prefix String of at least two Strings ; Function to find Max length of the prefix ; Base case ; Iterating over all the alphabets ; Checking if char exists in current String or not ; If atleast 2 String have that character ; Recursive call to i + 1 ; Driver code ; Initialising Strings ; push Strings into vectors .
using System ; using System . Collections . Generic ; class GFG { static int max1 = 0 ; static int MaxLength ( List < String > v , int i , int m ) { if ( i >= m ) { return m - 1 ; } for ( int k = 0 ; k < 26 ; k ++ ) { char c = ( char ) ( ' a ' + k ) ; List < String > v1 = new List < String > ( ) ; for ( int j = 0 ; j < v . Count ; j ++ ) { if ( v [ j ] [ i ] == c ) { v1 . Add ( v [ j ] ) ; } } if ( v1 . Count >= 2 ) { max1 = Math . Max ( max1 , MaxLength ( v1 , i + 1 , m ) ) ; } else { max1 = Math . Max ( max1 , i - 1 ) ; } } return max1 ; } public static void Main ( String [ ] args ) { String s1 , s2 , s3 , s4 , s5 ; s1 = " abcde " ; s2 = " abcsd " ; s3 = " bcsdf " ; s4 = " abcda " ; s5 = " abced " ; List < String > v = new List < String > ( ) ; v . Add ( s1 ) ; v . Add ( s2 ) ; v . Add ( s3 ) ; v . Add ( s4 ) ; v . Add ( s5 ) ; int m = v [ 0 ] . Length ; Console . Write ( MaxLength ( v , 0 , m ) + 1 ) ; } }