private static String commonPrefix(String a,String b){
  StringBuilder pref=new StringBuilder();
  int lenA=a.length();
  int lenB=b.length();
  int i=0;
  while (i < lenA && i < lenB) {
    if (a.charAt(i) == b.charAt(i))     pref.append(a.charAt(i));
 else     break;
    i++;
  }
  return pref.toString();
}

private static String commonPrefix ( String a , String b ) { StringBuilder pref = new StringBuilder ( ) ; int lenA = a . length ( ) ; int lenB = b . length ( ) ; int i = 0 ; while ( i < lenA && i < lenB ) { if ( a . charAt ( i ) == b . charAt ( i ) ) pref . append ( a . charAt ( i ) ) ; else break ; i ++ ; } return pref . toString ( ) ; }
get length of two strings loop over each char till one length runs out if same char , append it to result if not same , break return result
