由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 贡献一个朋友在Google的面题一枚。
相关主题
问一个Pinterest的题目yelp一题,攒rp
Ask a google interview question(3)Longest common string问题
问一个面试问题leetcode online judge Longest Palindromic Substring memory limit exceeded
finds all repeated substrings in the string --- YAHOO interview question Memory Limit Exceeded: Longest Palindromic Substring
longest repeated substring怎么做?(亚麻刚刚被问到的题)有人同看Longest Palindromic Substring 这道题么?
专家们,find the longest common substring of two stringscloudera的codebility的 test
longest common prefix 和 longest common substringfind longest subarray with the equal number of 0's, 1's
求助一道 Longest Common Substring 的变形面试题问个google老题的最佳解法
相关话题的讨论汇总
话题: startindex话题: string话题: achar话题: bchar话题: needlength
进入JobHunting版参与讨论
1 (共1页)
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
4
for
这个没看懂,是让干嘛啊?
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();

1 (共1页)
进入JobHunting版参与讨论
相关主题
问个google老题的最佳解法longest repeated substring怎么做?(亚麻刚刚被问到的题)
请教一道题目专家们,find the longest common substring of two strings
Find consecutive repeated stringlongest common prefix 和 longest common substring
G phone interview求助一道 Longest Common Substring 的变形面试题
问一个Pinterest的题目yelp一题,攒rp
Ask a google interview question(3)Longest common string问题
问一个面试问题leetcode online judge Longest Palindromic Substring memory limit exceeded
finds all repeated substrings in the string --- YAHOO interview question Memory Limit Exceeded: Longest Palindromic Substring
相关话题的讨论汇总
话题: startindex话题: string话题: achar话题: bchar话题: needlength