检查字符串是否按顺序包含子字符串的字符序列,但不一定紧挨着

Check if string contains the substring's sequence of characters in order, but not necessarily right next to each other

本文关键字:字符串 不一定 字符 是否 顺序 包含 检查      更新时间:2023-10-16

给定两个字符串,如果第一个字符串按顺序包含第二个字符串的字符序列,则返回 true,但不一定紧挨着。

例如,字符串:"

我很饿,现在想要食物",子字符串:"mto"。句子中 o 在 t 之前出现的子字符串不算数,它们必须按顺序排列,但不一定紧挨着。

我并不是真的在寻找代码,只是一般来说你会如何解决这个问题!

通常,当你第一次遇到问题时,你应该评估你自己如何解决问题,然后再考虑如何对计算机进行编程。

如果您尝试自己解决示例问题,则很可能会从第二个字符串中的第一个字符"m"开始,然后搜索该字符在字符串中的首次出现。一旦在第 3 个索引位置找到"m",您将从第 4 个索引开始求值以找到子字符串中的下一个字母。您将继续评估,直到发生以下两种情况之一:

  1. 您找不到其中一个字母。这意味着结果是 false .
  2. 子字符串中的字母用完了。这意味着结果是 true .

如果您了解如何自己解决问题,只需将解决方案分解为步骤即可。

你没有要求它,但在偶然的情况下,它使它更清楚,这里有一个简单的方法来解决问题:

public static boolean sub(String string, String substring) {
    // Keep track of our position in the string.
    int index = 0;
    // Iterate through all of the characters in the substring.
    for (char character : substring.toCharArray()) {
        // Find the current character starting from the last character we stopped on.
        index = string.indexOf(character, index);
        // If the method returned -1, the character was not found, so the result is false.
        if (index == -1)
            return false;
    }
    // If we reach this point, that means all characters were found, so the result is true.
    return true;
}

一个简单的实现就像

将两个字符串从一侧遍历到另一侧。如果找到匹配的字符,请在两个字符串中继续前进。否则,仅在 s2 中继续前进。

bool isSubSequence(std::string s1, std::string s2) {
    int j = 0;
    for (int i = 0; i < s2.length() && j < s1.length(); ++i)
        if (s1[j] == s2[i])
            ++j;
    return j == s1.length();
}

我建议使用正则表达式来解决这个问题。将子字符串转换为有效的 java 正则表达式。示例 mto 可以转换为类似这样的东西.*m.*t.*o.*然后你可以使用 Java PatternMatcher .

(注意:正则表达式中的.*表示任何字符零次或多次。字符前面的表示正则表达式解析器考虑字符的文字值)

也许只是使用 O(N) 贪婪算法?

正如您所说,您不需要代码,我将步骤描述如下:让S成为字符串,P成为模式

  1. 保持指向P PT指针,最初指向P的前面
  2. 对于 S 中的每个字符,检查 S[i] 是否等于 P 中的字符,并指向PT
  3. 如果是,请PT移动到P的下一个字符,如果它是您找到的最后一个字符
您必须一次

搜索一个子字符串字符的字符串。既然你说你不需要代码,我将尝试用文字解释如何(通过一些Java API调用作为参考)。

对于子字符串中的每个字符,在字符串中找到该字符的第一个匹配项(在本例中为"m",然后是"t",然后是"o")。您可以通过调用 String.indexOf(String char) 来执行此操作。跟踪找到该字符的索引,并在 String 中搜索下一个字符(在本例中为"t")时,使用 String.substring(idx, -1).indexOf(String char) 查找从该索引开始的字符串子集。-1 将从先前找到的字符 ('m') 的索引进行搜索,直到字符串的末尾。

继续迭代子字符串的字符,会发生以下两种情况之一。首先是您将在 String 的子集中找不到该字符,这意味着子字符串不按顺序排列。第二种可能性是,您将来到子字符串的末尾,并且将按顺序找到所有字符。

Q 1 : Substring Validation
Determine whether the second array is a subsequence of the first array, given two non-empty sequences of integers.
write a method.
A subsequence of a sequence is not necessarily contiguous in the sequence, but the same as they appear in the sequence.
is a sequenced set of numbers. For example, the numbers 1, 3, 4 form a substring of the array [1, 2, 3, 4], and
So are the numbers [2, 4]. A single number in a sequence and the sequence itself are not valid subsequences of the sequence.
Example Input
array= [5, 1, 22, 25, 6, -1, 8, 10]
sequence= [1, 6, -1, 10]
Sample Output
true
public static bool Validate Subarray(List<int> array, List<int> sequence) {
// the code will be written here
return false;
}