用例1:
输入 I love you
He
输出 He
说明:从用户自己输的的英文单词中找不到He前缀,故原样输出前缀
用例2:
输入: The furthest distance in the world, Is not between life and death, But when I stand in front of you, Yet you don’t know that I love you.
f
输出:front furthest
说明:从句子中提炼出的单词,符合“f”作为前缀的,有“furthest”和“front”,按字典序排序并在单词间添加空格后输出,结果为“front furthest”。
用例3:
输入: I don’t like you
d
输出 don
用例4:
输入: I don’t like you
t
输出 t
解法:
function solution(str, pre) {
return (
[...new Set([...str.split(/[^a-zA-Z]/)])]
.filter((item) => item.startsWith(pre))
.join(" ") || pre
);
}
console.log(
solution(
"The furthest distance in the world, Is not between life and death, But when I stand in front of you, Yet you don’t know that I love you.",
"f"
)
);