Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 11, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/3edcf037b8a6824a482d to your computer and use it in GitHub Desktop.
Save thmain/3edcf037b8a6824a482d to your computer and use it in GitHub Desktop.
public class LongestPrefixSequence {
private String[] arrA;
public LongestPrefixSequence(String[] arrA) {
this.arrA = arrA;
}
public String findPrefix() {
int resultLen = arrA[0].length();
int curr;
for (int i = 1; i < arrA.length; i++) {
curr = 0;
while (curr < resultLen && curr < arrA[i].length()
&& arrA[0].charAt(curr) == arrA[i].charAt(curr)) {
curr++;
}
resultLen = curr;
}
return arrA[0].substring(0, resultLen);
}
public static void main(String args[]) {
String x = "Sumit Summation Summit Sum";
String[] arrA = x.split(" ");
LongestPrefixSequence lp = new LongestPrefixSequence(arrA);
System.out.println("Original String : " + x);
System.out.println("Common Prefix is : " + lp.findPrefix());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment