Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
PRATHAP KUDUPU
authored and
PRATHAP KUDUPU
committed
May 15, 2017
1 parent
6f18771
commit 7fd161a
Showing
4 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package Algorithms.Strings; | ||
/** | ||
* Longest common Prefix | ||
* @author Prathap Kudupu | ||
* | ||
*/ | ||
public class LongestCommonPrefix { | ||
public static String get(String [] strs) | ||
{ | ||
//Check if the string array is null or the length is zero | ||
if (strs.length==0 || strs==null ) return ""; | ||
|
||
//Get the first string in the array of strings | ||
String pre=strs[0]; | ||
int i=1; | ||
//iterate till the end of the array | ||
while(i <strs.length) | ||
{ | ||
//We are taking the second string and comparing if index of previous returns 0 | ||
//If it returns zero we know that its common prefix | ||
while(strs[i].indexOf(pre)!=0) | ||
pre=pre.substring(0, pre.length()-1); | ||
i++; | ||
} | ||
return pre; | ||
} | ||
|
||
} |
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