m*******m 发帖数: 82 | 1 Interview Questions:
Redefine a function (signature given) to write data to a new replacement for
an antiquated database (which you previously designed)
Answer Question:
Write a function to return the longest common prefix between two strings.
//java code
String GetCommonPrefix(String a, String b)
{
char[] aChar = a.toCharArray();
char[] bChar = b.toCharArray();
int startIndex = 0;
//choose short length as the end index
int needLength= aChar.length>bChar.length?bChar.length:aChar.length;
while(startIndex
{
if(aChar[startIndex]!=bChar[startIndex])//find different!
break;
else
startIndex++;
}
return a.subString(0, startIndex);
}
// longest prefix
public String longestPrefix(String s1, String s2) {
int iCommon = 0;
for (int i = 0; i <= Math.min(s1.length(),s2.length());i++) {
if (s1.charAt(i) != s2.charAt(i)) {
break;
}else {
iCommon++;
}
}
return s1.substring(0,iCommon);
}
如果答得不好,求教正确的做法。谢谢! |
p*****2 发帖数: 21240 | 2 i <= Math.min(s1.length(),s2.length())
<=? |
w****o 发帖数: 2260 | 3 One mistake in the code:
//java code
String GetCommonPrefix(String a, String b)
{
char[] aChar = a.toCharArray();
char[] bChar = b.toCharArray();
int startIndex = 0;
//choose short length as the end index
int needLength= aChar.length>bChar.length?bChar.length:aChar.length;
while(startIndex
{
if(aChar[startIndex]!=bChar[startIndex])//find different!
break;
else
startIndex++;
}
return a.subString(0, startIndex);
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
should be return a.subString(0, startIndex-1);
for
【在 m*******m 的大作中提到】 : Interview Questions: : Redefine a function (signature given) to write data to a new replacement for : an antiquated database (which you previously designed) : Answer Question: : Write a function to return the longest common prefix between two strings. : //java code : String GetCommonPrefix(String a, String b) : { : char[] aChar = a.toCharArray(); : char[] bChar = b.toCharArray();
|
d******u 发帖数: 397 | |
p*****2 发帖数: 21240 | 5
are you sure?
【在 w****o 的大作中提到】 : One mistake in the code: : //java code : String GetCommonPrefix(String a, String b) : { : char[] aChar = a.toCharArray(); : char[] bChar = b.toCharArray(); : int startIndex = 0; : //choose short length as the end index : int needLength= aChar.length>bChar.length?bChar.length:aChar.length; : while(startIndex
|
s******o 发帖数: 2233 | 6 looks like Java's subString specifies the begin and end index,
while C++ string's substring specifies begin index and length.
【在 p*****2 的大作中提到】 : : are you sure?
|
r*****b 发帖数: 310 | 7 I guess the second question is on the longest common substr of two strings.
Otherwise, why we need the second function?
Here is a post that has the pseudo-code on computing the longest common
substr:
http://basicalgos.blogspot.com/2012/03/17-longest-common-substr
for
【在 m*******m 的大作中提到】 : Interview Questions: : Redefine a function (signature given) to write data to a new replacement for : an antiquated database (which you previously designed) : Answer Question: : Write a function to return the longest common prefix between two strings. : //java code : String GetCommonPrefix(String a, String b) : { : char[] aChar = a.toCharArray(); : char[] bChar = b.toCharArray();
|