查找包含另一个字符串作为子序列的最小子字符串的长度

时间:2020-05-05 08:57:51

标签: string algorithm data-structures time-complexity

假设我有一个字符串[[ 1 0 1 2 3 4] [ 1 5 6 7 8 9] [ 1 10 11 12 13 14] [ 1 15 16 17 18 19] [ 1 20 21 22 23 24]] 和另一个字符串s1 = "eabegcghdefgh"

由于s2 = "egh"的子字符串"efgh"(因为它是包含s1作为子序列的最小子字符串),因此代码应返回答案 4

注意:可能有几个子字符串,但是s2是最小的子字符串。

换句话说,找到"efgh"的最小子字符串的长度,该子字符串包含另一个字符串s1的所有字符,但顺序正确。

请注意:我想问一下在O(n)时间复杂度下该怎么做。

2 个答案:

答案 0 :(得分:0)

public static String smallestWindow(String S, String T) {
    if (S.equals(T))  //If S and T are equal, then simply return S.
        return S;
    /**
     * Use sliding window. 

如果 S 的子串 W 存在使得 T 是 W 的子序列, 那么 S 的一个比 W 长或与 W 长度相同的子串也必须存在。 因此,从 S 中的索引 0 和 T 中的索引 0 开始,找到 S 的这样一个子串。 如果到达 T 的最后一个字符,则 S 中的当前索引为子串的结束索引,并找到 S 中子串的最大可能起始索引。 找到开始索引和结束索引后,可以更新最小窗口大小,同时存储开始索引和结束索引。

然后将 T 中的索引设置为 0 并尝试在 S 中找到另一个子字符串。 重复这个过程,直到到达 S 的末尾。 访问完S中的所有字符后,可以得到最小子串长度,并返回最短的子串。

     */
    int sLength = S.length(), tLength = T.length();
    int start = 0, end = sLength - 1;
    int sIndex = 0, tIndex = 0;
    while (sIndex < sLength) {
        if (S.charAt(sIndex) == T.charAt(tIndex))
            tIndex++;
        if (tIndex == tLength) {
            int rightIndex = sIndex;
            tIndex--;
            while (tIndex >= 0) {
                if (S.charAt(sIndex) == T.charAt(tIndex))
                    tIndex--;
                sIndex--;
            }
            sIndex++;
            if (rightIndex - sIndex < end - start) {
                start = sIndex;
                end = rightIndex;
            }
            tIndex = 0;
        }
        sIndex++;
    }
    int windowSize = end - start + 1;
        return S.substring(start, start + windowSize);
}

答案 1 :(得分:-1)

这是一个基本的滑动窗口问题,通常称为“包含另一个字符串的所有字符的字符串中的最小窗口”。我们通过从要搜索的字符串的第一个索引开始有两个指针“ low”和“ high”来解决此问题,然后递增高指针直到模式的所有字符都匹配,然后尝试通过递增低指针。

这是该问题的Java代码段。

<div class="moves-row">
  <button class="dropdown-button">
      <span class="dropdown-button-image"></span>
    </button>
  <div class="moves-row-detail-0 hidden" style="opacity: 1;transform: scaleY(0.01);">
    <div class="moves-row-stats"><span><strong>Power:</strong> 40</span><span><strong>Acc:</strong>
          100%</span><span><strong>PP:</strong> 35</span></div>
    <div class="move-description">Inflicts regular damage.</div>
  </div>
</div>

<div class="moves-row">
  <button class="dropdown-button">
      <span class="dropdown-button-image"></span>
    </button>
  <div class="moves-row-detail-1 hidden" style="opacity: 1; transform: scaleY(0.01);">
    <div class="moves-row-stats"><span><strong>Power:</strong> N/A</span><span><strong>Acc:</strong>
          100%</span><span><strong>PP:</strong> 40</span></div>
    <div class="move-description">Lowers the target's Attack by one stage.</div>
  </div>
</div>
<div class="moves-row">
  <button class="dropdown-button">
      <span class="dropdown-button-image"></span>
    </button>
  <div class="moves-row-detail-2 hidden" style="opacity: 1; transform: scaleY(0.01);">
    <div class="moves-row-stats"><span><strong>Power:</strong> 40</span><span><strong>Acc:</strong>
          100%</span><span><strong>PP:</strong> 25</span></div>
    <div class="move-description">Inflicts regular damage. Has a 10% chance to burn the target.</div>
  </div>
</div>

我建议您观看此视频以了解它。 Smallest window in a string containing all the characters of another string