老师,我这个算法不知道如何解决,思路也没有

来源:2-6 均摊时间复杂度分析(Amortized Time Analysis)

qq_001秒_0

2019-04-23

An array of N words is given. Each word consists of small letters (‘a’ − ‘z’). Our goal is to concatenate the words in such a way as to obtain a single word with the longest possible substring composed of one particular letter. Find the length of such a substring.

Write a function:

func Solution(words []string) int

that, given an array words containing N strings, returns the length of the longest substring of a word created as described above.

Examples:

  1. Given N=3 and words=[“aabb”, “aaaa”, “bbab”], your function should return 6. One of the best concatenations is words[1] + words[0] + words[2] = “aaaaaabbbbab”. The longest substring is composed of letter ‘a’ and its length is 6.

  2. Given N=3 and words=[“xxbxx”, “xbx”, “x”], your function should return 4. One of the best concatenations is words[0] + words[2] + words[1] = “xxbxxxxbx”. The longest substring is composed of letter ‘x’ and its length is 4.

  3. Given N=4 and words=[“dd”, “bb”, “cc”, “dd”], your function should return 4. One of the best concatenations is words[0] + words[3] + words[1] + words[2] = “ddddbbcc”. The longest substring is composed of letter ‘d’ and its length is 4.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1…100,000];
all the words are non−empty and consist only of lowercase letters (a−z);
S denotes the sum of the lengths of words; S is an integer within the range [1…100,000].

写回答

1回答

liuyubobobo

2019-04-24

非常抱歉,我不能因为讲了一个算法课程,同学们一有各种各样的算法问题就进行解答。如果是那样的话,我就不能干别的了。请谅解。


这个课程的问答区,我可以对课程中的内容,另外包括Leetcode中的所有问题进行答疑。Leetcode题库本身已经够大了了,足够覆盖近乎所有面试中遇到的传统算法问题。对于其他的问题,请去相关社区,或者寻找出题人解答。请谅解。


如果你的问题是Leetcode上的问题,请给我题号就可以:)


加油!:)


1
0

玩转算法面试-- Leetcode真题分门别类讲解

课程配套大量BAT面试真题,高频算法题解析,强化训练

7410 学习 · 1150 问题

查看课程