javascript - 找出一个字符串在一组字符串中变得唯一之前需要多少
问题描述
这很难解释,但我会尽力...
我有一个字符串数组。让我们在这里使用一个例子:
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 friend robert”返回“hello there my”。这是因为“你好”本身是模棱两可的。它可以指“你好,我的朋友罗伯特”或“你好,朋友金伯利”。然而,“你好,我的”只能指“你好,我的朋友罗伯特”。
我完全不知道如何做到这一点。还有其他人知道解决方案吗?
(为简单起见,假设输入字符串没有标点符号并且完全小写。)
解决方案
我想我对此有一些想法。你可以做的就是像这样存储列表。
{
'first' : ['hello', 'hello', 'hello', 'hi'],
'second' : ['there','friend','there'],
'third' : ['my', 'friend', 'claire'],
'fourth' : ['robert', 'kimberly', 'chris']
}
现在只需检查这张地图上第一个的计数是否超过一个,那么你必须继续前进,如果计数是一个,你必须停在那里。
推荐阅读
- javascript - UglifyJs 意外标记:关键字«const» Webpack 4
- c# - 自 16.2.x 起,Visual Studio 2019 测试资源管理器切换到“OR”过滤
- android - 如何在 build.gradle 文件中使用 assemblerelease
- python - 无法在 Tkinter 的主窗口中添加滚动条
- elasticsearch - 如何在 Elasticsearch 中优先考虑第一个单词匹配而不是第一个字符?
- haskell - 如何将树数据存储在列表中?
- bash - 将 awk 输出分配给变量
- database - 具有迁移策略的开放资产数据库
- flutter - 添加 Firestore 文档时更新 ListView.builder itemCount
- node.js - Javascript/typescript 如何将 pgm 转换为 png 或 jpg?