Check If a Word Occurs As a Prefix of Any Word in a Sentence

EasyTwo PointersStringString Matching

Solution

export function isPrefixOfWord(sentence: string, searchWord: string): number {
  const words = sentence.split(' ');
  const result = words.findIndex((word) => word.startsWith(searchWord));
  return result === -1 ? result : result + 1;
}