Given input array of Strings strArr, find the common sequence of characters that is present at start (prefix) of each of the Strings in the array and return the prefix.
| Input Parameters |
Expected outputs |
| The input string array is:
autopilot, automatic, autofocus, autobiography
|
Longest common prefix: auto
|
| The input string array is:
copilot, coworker, cooperate
|
Longest common prefix: co
|
| The input string array is:
house, plane, pledge
|
No common prefix exists
|
| The java method should accept following input parameters: strArr (String[]).
|
| Set String variable pre=strArr[0], this sets pre to first String of the array.
|
| Using a for loop, search whether each of the other words in strArr begins with pre. If yes, pre is the longest common prefix.
|
| Else we will remove one last character from pre and set the remaining string without last character to pre again.
|
| Now repeat above step till either pre is present at start of all Strings in strArr or until pre become blank.
|
| If pre becomes blank, it means no common prefix exists.
|
| Return pre.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class StringLongestCommonPrefix {
public String stringLongestCommonPrefix(String[] strArr) {
String pre=strArr[0];
for (int idx=1; idx < strArr.length; idx++) {
while ((!pre.equals("") && (strArr[idx].startsWith(pre)==false))) {
pre=pre.substring(0, pre.length()-1);
}
}
return pre;
}
public static void main(String[] args) {
StringLongestCommonPrefix sp=new StringLongestCommonPrefix();
String retVal;
try {
String[] strArr= {"autopilot", "automatic", "autofocus", "autobiography"};
System.out.println("\n\nThe original string is: \n");
for (int idx=0; idx < strArr.length; idx++) {
System.out.print(strArr[idx]);
if (idx != strArr.length-1) {
System.out.print(",");
}
}
retVal=sp.stringLongestCommonPrefix(strArr);
System.out.println("\nLongest common prefix: "+retVal);
String[] strArr1= {"copilot", "coworker", "cooperate"};
System.out.println("\n\nThe original string is: \n");
for (int idx=0; idx < strArr1.length; idx++) {
System.out.print(strArr1[idx]);
if (idx != strArr1.length-1) {
System.out.print(",");
}
}
retVal=sp.stringLongestCommonPrefix(strArr1);
System.out.println("\nLongest common prefix: "+retVal);
String[] strArr2= {"friend", "follow", "first"};
strArr1=strArr2;
System.out.println("\n\nThe original string is: \n");
for (int idx=0; idx < strArr1.length; idx++) {
System.out.print(strArr1[idx]);
if (idx != strArr1.length-1) {
System.out.print(",");
}
}
retVal=sp.stringLongestCommonPrefix(strArr1);
System.out.println("\nLongest common prefix: "+retVal);
String[] strArr3= {"house", "plane", "pledge"};
strArr1=strArr3;
System.out.println("\n\nThe original string is: \n");
for (int idx=0; idx < strArr1.length; idx++) {
System.out.print(strArr1[idx]);
if (idx != strArr1.length-1) {
System.out.print(",");
}
}
retVal=sp.stringLongestCommonPrefix(strArr1);
System.out.println("\nLongest common prefix: "+retVal);
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
}