add: 检查单词是否为句中其他单词的前缀
This commit is contained in:
parent
4940ceaeb9
commit
5c5f35c535
@ -109,6 +109,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
||||
- LeetCode 76. 最小覆盖子串 <https://leetcode-cn.com/problems/minimum-window-substring/>
|
||||
- LintCode 32. 最小子串覆盖 <https://www.lintcode.com/problem/minimum-window-substring/description>
|
||||
|
||||
- [检查单词是否为句中其他单词的前缀](src/string/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.js)
|
||||
|
||||
- LeetCode 5416. 检查单词是否为句中其他单词的前缀 <https://leetcode-cn.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/>
|
||||
|
||||
## 数组/队列/集合/映射
|
||||
|
||||
- [电话号码的字母组合](src/array/letter-combinations-of-a-phone-number.js)
|
||||
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @param {string} sentence
|
||||
* @param {string} searchWord
|
||||
* @return {number}
|
||||
*/
|
||||
export const isPrefixOfWord = function (sentence, searchWord) {
|
||||
const items = sentence.split(' ')
|
||||
|
||||
for (const n in items) {
|
||||
if (items[n].indexOf(searchWord) === 0) return Number(n) + 1
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
import { isPrefixOfWord } from '../../src/string/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence'
|
||||
|
||||
test('检查单词是否为句中其他单词的前缀', () => {
|
||||
expect(isPrefixOfWord('i love eating burger', 'burg')).toBe(4)
|
||||
})
|
Loading…
Reference in New Issue
Block a user