在字符串中查找子字符串
Posted
技术标签:
【中文标题】在字符串中查找子字符串【英文标题】:Finding subString in String 【发布时间】:2022-01-22 14:36:32 【问题描述】:我正在尝试使用 O(N) 复杂度。以下是我编写的代码。它返回未定义,我不知道为什么。请让我知道我的代码出了什么问题。
let omg = "omg";
let omgi = "omjiklmonomgib";
function stringSearch(smallString, bigString)
let left = 0;
let right = left+(smallString.length - 1);
while (left > right)
if (smallString[left] === bigString[left])
if (smallString[right] === bigString[right])
left++;
right--;
if (left === right)
if (smallString[right] === bigString[left])
return true;
else if (right === left + 1)
if (smallString[right] === bigString[right] && smallString[left] === bigString[left])
return true;
else
left = right + 1;
right = left + (smallString.length - 1);
console.log(stringSearch(omg, omgi)); //Undefined
【问题讨论】:
您需要在最后添加一个 return 语句,或者至少为每个条件分支添加一个,因为此时您的代码正在到达一个没有 return 语句的分支。 你也可以使用omgi.split( omg )
如果你最终得到不止一件,你至少有一个匹配。
【参考方案1】:
据我了解,你只是在改造String.prototype.match。 尝试检查一下,因为这可能是执行您所说的更简单的方法。抱歉,错过了 O(N) 复杂性部分。
如果你真的想定制一个,我可以给你一些建议。
首先,您应该有一个“缓存”变量(一个代表所有变量,而不是 left
和 right
)和另一个变量 found
(它将是一个布尔值,因此将其设置为 false)。 “缓存”变量将存储文本,另一个将存储您是否找到smallString
。
基本上,您循环遍历每个字符并将smallString
长字符存储在“缓存”变量中。一旦“缓存”变量的长度与smallString
相同,就对其运行 if 语句。如果不等于smallString
,则删除“缓存”的第一个字符。循环中的下一次迭代,它将添加另一个字符。然后你和以前一样,运行一个 if 语句,如果它不等于删除第一个字符并继续循环,直到你找到它,或者字符串结束。如果找到了,请将布尔值设置为 true。
类似这样的:
function stringSearch(smallString, bigString, caseSensitive=true)
if(!caseSensitive) // if caseSensitive is false, make everything lower case
smallString = smallString.toLowerCase();
bigString = bigString.toLowerCase();
let cache = ""; // string cache
let found = false; // result
for(i=0;i<bigString.length;i++) // loop through every character in bigString
cache+=bigString[i]; // add the current character to the cache
if(cache.length == smallString.length) // check if the cache's length is the same as the smallString's length
if(cache == smallString) // check if the cache is equal to the smallString
found = true; // set found to true
break; // break the loop (stop it from going on)
else
cache = cache.substring(1); // set cache to itself but remove the first character
return found; // return result
// example:
console.log("String 'hello, world' has 'hello': "+stringSearch("hello", "hello, world"));
console.log("String 'HELLO WORLD' has 'hello': "+stringSearch("hello", "HELLO WORLD"));
console.log("String 'HELLO WORLD' has 'hello' (not case sensitive): "+stringSearch("hello", "HELLO WORLD", false));
console.log("String 'hey hi hello WORLD' has 'hello': "+stringSearch("hello", "hey hi hello WORLD"));
console.log("String 'hey hi hello' has 'hello': "+stringSearch("hello", "hey hi hello"));
【讨论】:
【参考方案2】:我看到了几个问题。首先,代码没有针对每个逻辑分支的返回语句。我的意思是,对于 if、then、else 语句中的每个条件,代码都应该有一个 return 语句或某种控制流语句(例如递归调用或 continue),它们最终会导致一个 return 语句。
第二个问题是while
循环。假设right
从函数的开始就应该大于left
(除非smallString
的长度为0)因为right
是smallerString
的长度减1。while
的条件是@ 987654328@,因此除非smallString
具有负长度(它没有),否则不会执行任何时间。
顺便说一句,如果你想检查整个bigString
,你需要遍历bigString
,而不是smallString
。如果您只检查smallString.length
字符,则不会检查整个bigString
。弄清楚如何编写这个函数是一个很好的练习,所以我将把它的编写留给你,并避免自己提供实现。继续努力!
【讨论】:
以上是关于在字符串中查找子字符串的主要内容,如果未能解决你的问题,请参考以下文章