gpt4 book ai didi

algorithm - 按 char 字符串的顺序检查 chars 子集

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:23:19 27 4
gpt4 key购买 nike

全部:

我想检查 string1 是否按顺序包含 string2 中的所有字符,我想知道我该怎么做?

For example:

string1 = "abcdeda"
string2 = "ba"

it will return true because by removing first a and cded you can left ba which matches.

谢谢

最佳答案

index = -1
str1 = "abcdeda"
str2 = "ba"

for c in str2
tmp = indexOf(c , str1 , index)
if tmp != -1
index = tmp
else
return //no match at c

indexOf(c , str , index): first index of c in str after index, or -1, i
if no occurence was found

关于algorithm - 按 char 字符串的顺序检查 chars 子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808277/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com