【问题标题】:problem definition not clear Longest common prefix问题定义不清楚 最长公共前缀
【发布时间】: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 怎么样?

标签: algorithm prefix


【解决方案1】:

你需要找到两个字符串之间最长的公共子字符串。在每种情况下,其中之一是 s,即原始字符串。另一个字符串是s 的右端子字符串。这些是第一个列表。

几个例子:

substring         common   len   reason
s(1, 6) = ababac  ababac    6     Comparing the string with itself yields the entire string
s(3, 6) = abac    aba       3     Only the first 3 characters match
s(4, 6) = bac     -none-    0     The first characters differ, so there is no common prefix.

这有帮助吗?

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 1970-01-01
    • 2022-11-22
    • 2018-09-30
    • 2013-04-14
    • 2012-02-01
    • 2020-07-05
    • 2020-02-10
    • 2022-01-11
    相关资源
    最近更新 更多