检查字符串是否按顺序包含子字符串的字符序列,但不一定紧挨着
Check if string contains the substring's sequence of characters in order, but not necessarily right next to each other
给定两个字符串,如果第一个字符串按顺序包含第二个字符串的字符序列,则返回 true,但不一定紧挨着。
例如,字符串:"我很饿,现在想要食物",子字符串:"mto"。句子中 o 在 t 之前出现的子字符串不算数,它们必须按顺序排列,但不一定紧挨着。
我并不是真的在寻找代码,只是一般来说你会如何解决这个问题!
通常,当你第一次遇到问题时,你应该评估你自己如何解决问题,然后再考虑如何对计算机进行编程。
如果您尝试自己解决示例问题,则很可能会从第二个字符串中的第一个字符"m"开始,然后搜索该字符在字符串中的首次出现。一旦在第 3 个索引位置找到"m",您将从第 4 个索引开始求值以找到子字符串中的下一个字母。您将继续评估,直到发生以下两种情况之一:
- 您找不到其中一个字母。这意味着结果是
false
. - 子字符串中的字母用完了。这意味着结果是
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 Pattern
和 Matcher
.
(注意:正则表达式中的.*
表示任何字符零次或多次。字符前面的表示正则表达式解析器考虑字符的文字值)
也许只是使用 O(N) 贪婪算法?
正如您所说,您不需要代码,我将步骤描述如下:让S
成为字符串,P
成为模式
- 保持指向
P
PT
指针,最初指向P
的前面 - 对于
S
中的每个字符,检查S[i]
是否等于P
中的字符,并指向PT
- 如果是,请
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;
}
- 类中的字符串不命名类型
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- C++ - 使用基于范围的 for 循环将字符值分配给向量中的字符串不会分配值
- 反转字符串.不知道为什么这个逻辑是错误的.C++
- 如何保护非托管应用程序中的字符串不受进程转储的影响
- C++|以一种很好的方式将树(不一定是二进制的)打印到stdout
- 修改向量中的字符串?(不使用循环)
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- C ++相同的字符串不相等(实际上是char*)
- 不一定获得成员价值的 getter 方法?
- 将字符从一个字符串简单分配到另一个字符串不起作用
- 为什么字符串不会在堆栈中推送或弹出
- boost::split会在输入字符串不是常量时更改输入字符串吗?
- 将字符指针按顺序存储在 map 中 std::map<char*, int> mymap。将其存储为字符或字符串不是一个选项
- C++实现代码中的字符串不应存在于输出二进制文件中.如何解决
- 大小 N 和 N 的所有排列不一定等于数组的大小
- 字符串不会使用 reverse_copy 反转
- C++ cout 在擦除换行符后打印字符串不正确
- C 字符串不会重新分配
- 检查字符串是否按顺序包含子字符串的字符序列,但不一定紧挨着