【发布时间】:2021-06-13 20:53:11
【问题描述】:
这很难解释,但我会尽力...
我有一个字符串数组。让我们在这里举个例子:
var myArray = [
"hello there my friend robert"
"hello there friend kimberly"
"hello friend claire"
"hi there friend chris"
]
我正在尝试编写一个函数,该函数将确定数组中每个字符串在变得唯一之前需要多少个单词。例如,使用上述数组作为输入,函数应返回以下内容:
[
"hello there my",
"hello there friend",
"hello friend",
"hi"
]
返回数组中的字符串表示在字符串变得唯一之前每个字符串所需的单词。
例如,该函数为“你好,我的朋友罗伯特”返回“你好,我的”。这是因为“你好”本身是模棱两可的。它可以指“你好,我的朋友罗伯特”或“你好,朋友金伯利”。但是,“hello there my”只能指“hello there my friend robert”。
我完全不知道如何做到这一点。还有其他人知道解决方案吗?
(为简单起见,假设输入字符串没有标点符号并且完全小写。)
【问题讨论】:
-
应该是
'hi',因为第一个字和其他字不一样 -
@YasharthDubey 是正确的。我试图找出从字符串开头开始需要多少个单词。由于 "chris" 位于字符串的末尾,因此不会使用它。
-
你有没有试过我的解决方案兄弟
标签: javascript arrays string