Created
June 11, 2015 21:59
-
-
Save thmain/3edcf037b8a6824a482d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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