大牛们,蓉2股神的1020这波还会来吗?# Stock
w*k
1 楼
Given a string, find the substring which occurs most frequently. This
substring must have length between K and L, and must have no more than M
distinct characters, assuming string has only lower case letters 'a' to 'z'.
--
解法很多,最优的是什么?
我的想法是类似于sliding window, 以每个字符为substring起点, 考虑长为K的子串
,将其中字符hash set以知道distinct chars是不是在M以内,然后将子串放入hash
map以累计频率;然后长度K++,放入新的字符更新hash set和map,当然只需要考虑
新字符带来的改变。起始位置++时也一样,只需考虑扔掉字符带来的改变。
因为要求用C,我只好自己写hash set 和 map, 还有 hash function,再加上的确对C
不熟,虽然C++那是杠杠的,没写完,如果用C++的话应该能完成,但不知道是不是
最优的。
substring must have length between K and L, and must have no more than M
distinct characters, assuming string has only lower case letters 'a' to 'z'.
--
解法很多,最优的是什么?
我的想法是类似于sliding window, 以每个字符为substring起点, 考虑长为K的子串
,将其中字符hash set以知道distinct chars是不是在M以内,然后将子串放入hash
map以累计频率;然后长度K++,放入新的字符更新hash set和map,当然只需要考虑
新字符带来的改变。起始位置++时也一样,只需考虑扔掉字符带来的改变。
因为要求用C,我只好自己写hash set 和 map, 还有 hash function,再加上的确对C
不熟,虽然C++那是杠杠的,没写完,如果用C++的话应该能完成,但不知道是不是
最优的。