diff --git a/src/page-27/2942. Find Words Containing Character/findWordsContaining.test.ts b/src/page-27/2942. Find Words Containing Character/findWordsContaining.test.ts new file mode 100644 index 0000000..1479666 --- /dev/null +++ b/src/page-27/2942. Find Words Containing Character/findWordsContaining.test.ts @@ -0,0 +1,9 @@ +import { findWordsContaining } from './findWordsContaining'; + +describe('2942. Find Words Containing Character', () => { + test('findWordsContaining', () => { + expect(findWordsContaining(['leet', 'code'], 'e')).toStrictEqual([0, 1]); + expect(findWordsContaining(['abc', 'bcd', 'aaaa', 'cbc'], 'a')).toStrictEqual([0, 2]); + expect(findWordsContaining(['abc', 'bcd', 'aaaa', 'cbc'], 'z')).toStrictEqual([]); + }); +}); diff --git a/src/page-27/2942. Find Words Containing Character/findWordsContaining.ts b/src/page-27/2942. Find Words Containing Character/findWordsContaining.ts new file mode 100644 index 0000000..44e6767 --- /dev/null +++ b/src/page-27/2942. Find Words Containing Character/findWordsContaining.ts @@ -0,0 +1,16 @@ +type FindWordsContaining = (words: string[], x: string) => number[]; + +/** + * Accepted + */ +export const findWordsContaining: FindWordsContaining = (words, x) => { + const result: number[] = []; + + for (let i = 0; i < words.length; i++) { + if (words[i].includes(x)) { + result.push(i); + } + } + + return result; +};