【发布时间】:2019-02-28 21:42:57
【问题描述】:
问题陈述: 在一次采访中,我遇到了一个我无法弄清楚的问题的编码挑战。
Given a string s contains lowercase alphabet, find the length of the Longest common Prefix of all substrings in O(n)
For example
s = 'ababac'
Then substrings are as follow:
1: s(1, 6) = ababac
2: s(2, 6) = babac
3: s(3, 6) = abac
4: s(4, 6) = bac
5: s(5, 6) = ac
6: s(6, 6) = c
Now, The lengths of LCP of all substrings are as follow
1: len(LCP(s(1, 6), s)) = 6
2: len(LCP(s(2, 6), s)) = 0
3: len(LCP(s(3, 6), s)) = 3
4: len(LCP(s(4, 6), s)) = 0
5: len(LCP(s(5, 6), s)) = 1
6: len(LCP(s(6, 6), s)) = 0
String contains only lowercase alphabates.
我在这个社区中发现了一些相同的问题,但它们只是问题陈述,其含义无法定义。 This one
其中一个leetcode问题也有点像我完全理解的代码:Longest common prefix 但我的问题听起来一样,但主要区别是所有这类问题都提供了一个字符串数组,但在我的问题中有一个字符串,我必须从这个字符串中找出最长的公共前缀。
谁能正确解释我这个问题到底在寻找什么?
【问题讨论】:
-
您有一个
Strings 数组:s的所有子字符串。通过一些观察,您可以争论为什么您只需要考虑形式为s(i, 6)(和[1, 6]中的i)的子字符串。 -
外围问题:你的问题
java-related 怎么样?