Skip to content

Commit

Permalink
Pre Consecutive Prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
PRATHAP KUDUPU authored and PRATHAP KUDUPU committed May 15, 2017
1 parent 6f18771 commit 7fd161a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Binary file added bin/Algorithms/Strings/LongestCommonPrefix.class
Binary file not shown.
Binary file modified bin/HomeString.class
Binary file not shown.
28 changes: 28 additions & 0 deletions src/Algorithms/Strings/LongestCommonPrefix.java
@@ -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;
}

}
16 changes: 15 additions & 1 deletion src/HomeString.java
Expand Up @@ -62,10 +62,24 @@ public static void main(String args[]){
M is a thousand, CM is nine hundred (100-1000)
and IV is four
*/
/*
String s="MCMIV";
int romanToInteger=Algorithms.Strings.RomanToInteger.get(s);
System.out.println("Detect Capitol : "+ romanToInteger);

*/
/**
* Longest Common Prefix
* Input : {"tommorow", "tommy", "tomandGerry", "tod"}
Output : "to"
*/
/*
*
*/
String [] str={"tommorow", "tommy", "tomandGerry", "tod"};

String longestCommonPrefix=Algorithms.Strings.LongestCommonPrefix.get(str);
System.out.println("Detect Capitol : "+ longestCommonPrefix);

}
}

0 comments on commit 7fd161a

Please sign in to comment.