In two strings:
\"Mary Had a Little Lamb\"
\"Mary Had a Big Lamb\"
should return
\"Mary Had a \"
I have written a sample code. Hope it will help you. Below is the Code.
import java.util.Arrays;
import java.util.Collections;
public class MaxPrefixString {
public static void main(String[] args) {
int count = 0;
String [] input= {"Java is a Programming Language",
"Java is a OO",
"Java is a Platform independent language",
"Java is a my favourite"};
Collections.sort(Arrays.asList(input));
Integer [] length = new Integer[input.length];
for(int j= 0; j<input.length ; j++){
length[j] = input[j].length();
}
Collections.sort(Arrays.asList(length));
for(int i=0;i<length[0];i++){
if(isValidPrefix(input[0].substring(0, i), input)){
count++;
}else{
break;
}
}
System.out.println("Answer:"+input[0].substring(0, count-1));
}
private static boolean isValidPrefix(String str,String []input){
boolean istrueForAll = true;
for(int i = 0;i<input.length;i++){
istrueForAll = istrueForAll && check(input[i], str);
}
return istrueForAll;
}
private static boolean check(String input,String str){
return input.contains(str);
}
}
Let me know if I missed any scenario. click here
Use binary search. Try to compare whole strings. If they are not equal try to compare the first characters. If they are equal try to split the strings (substring(0, str.length()/2
). Etc, etc.
String str1;
String str2;
// assuming str1.length > str2.length
A very not efficient and probably might be criticised a lot. But i came up with this idea and surprisingly it satisfied all of my test cases. And it even did the work that you were intending to do. Easy to understand. Please do comment on it and let me know how i can improve it. (open to all kinds of remarks and improvement)
class Solution {
public String longestCommonPrefix(String[] strs) {
int i;
StringBuilder sb = new StringBuilder("");
if(strs.length == 0 ){
return sb.toString();
}
for(int j = 0 ; j< strs[0].length();j++)
{
int count=0;
for(String word: strs){
try{
word.charAt(j);
}catch(Exception e){
return sb.toString();
}
if(word.charAt(j) == strs[0].charAt(j) ){
count ++;
}else{
return sb.toString();
}
if(count == strs.length){
sb.append(word.charAt(j));
}
}
}
return sb.toString();
}}
Apache Commons to the rescue!
org.apache.commons.lang3.StringUtils.getCommonPrefix
... and compare the source code with the ingenious/valiant effort of dyross here (currently with highest vote). But her/his code, good though it is, only handles two String
s. This can handle any number.
Apart from not re-inventing the wheel, I can think of two reasons why using Apache Commons is always best for this sort of thing.
If the whole of a given Apache Commons module is really too much for your context (they're usually just a few kB, but OK) you can extract the bits you need from the source code (assuming this complies with the licence). In this case indexOfDifference is an essential function...
public class Test{
public static void main(String[] args){
String s1 = "Mary Had a Little Lamb";
String s2 = "Mary Had a Big Lamb";
int minStrLen = s1.length();
if ( minStrLen > s2.length()){
minStrLen = s2.length();
}
StringBuilder output = new StringBuilder();
for(int i=0; i<minStrLen; i++){
if ( s1.charAt(i) == s2.charAt(i)){
output.append(s1.charAt(i));
}else{
break;
}
}
System.out.println(output.toString());
}
}
This may not be the optimum solution, but this is easy to understand and program.
I borrowed this idea from the list merging technique of merge-sort algorithm. If you read little about list merging technique you will better understand the logic of my algorithm.