由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一道题目
相关主题
帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "这道题咋做?
Dream company Onsite被搞了(少量面经)wordbreak in C?
讨论一道G的题find longest substring which contains just two unique characters.星期一福利:某公司店面题
Find shortest substring that is only occurring once. in Given String(Medallia面试题)G面经
问一道算法题max length of subsequence string matching subsGoogle onsite 题目求助
an interview question报一个F 家面经
谁能猜猜,这是个什么 algorithm?问道题string pattern match的题目
[合集] G家onsite面经求问一道面试题
相关话题的讨论汇总
话题: leftpos话题: string话题: word话题: length话题: iter
进入JobHunting版参与讨论
1 (共1页)
c***g
发帖数: 472
1
Given a word (assume every char is unique in this word) and a string, find
the length of the shortest snippet/substring that contains all characters of
the word. For example: with word "abc" and the string "a brilliant cat is
eating a big cake", the shortest snippet is"big ca", which has length 6.
我能想到的是,用两个index,keep 一个window,然后滑动,每当找到一个substring
满足后,后面的index就滑到下一个可能的字母那里去。
请问谁有更好的方法么?
k*****y
发帖数: 744
2
traverse一遍string,记当前位置是i;
用map pos记录word里面相应字母出现在i之前最后的位置;
如果word中每个字母都出现了,找出pos中位置最小的一个,就可以算出以i为结尾最短
的长度。
====================================
string getShortestSubstr(string &word, string &str) {
map pos;
int left, length=str.length()+1;
for( int i=0; i char ch = str[i];
if( word.find(ch) != string::npos ) {
pos[ch] = i;
if( pos.size() == word.size() ){
int leftPos = i;
for( map::iterator iter=pos.begin();iter!=pos.end(
); ++iter )
if( iter->second < leftPos )
leftPos = iter->second;
if( i-leftPos+1 < length )
left = leftPos, length = i-leftPos+1;
}
}
}
string substr;
if( pos.size() == word.size() )
substr = str.substr(left, length);
return substr;
}

of
substring

【在 c***g 的大作中提到】
: Given a word (assume every char is unique in this word) and a string, find
: the length of the shortest snippet/substring that contains all characters of
: the word. For example: with word "abc" and the string "a brilliant cat is
: eating a big cake", the shortest snippet is"big ca", which has length 6.
: 我能想到的是,用两个index,keep 一个window,然后滑动,每当找到一个substring
: 满足后,后面的index就滑到下一个可能的字母那里去。
: 请问谁有更好的方法么?

q******8
发帖数: 848
h********e
发帖数: 1972
4
two pointers with a counter of sizer 26, O(n) time.
1 (共1页)
进入JobHunting版参与讨论
相关主题
求问一道面试题问一道算法题max length of subsequence string matching subs
问一道最近的onsite题an interview question
这个怎么做?谁能猜猜,这是个什么 algorithm?
Substring with Concatenation of All Words 还有更简洁的解法吗?[合集] G家onsite面经
帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "这道题咋做?
Dream company Onsite被搞了(少量面经)wordbreak in C?
讨论一道G的题find longest substring which contains just two unique characters.星期一福利:某公司店面题
Find shortest substring that is only occurring once. in Given String(Medallia面试题)G面经
相关话题的讨论汇总
话题: leftpos话题: string话题: word话题: length话题: iter