LeetCode 1268. Search Suggestions System

Jan. 14, 2022

/**
 * @param {string[]} products
 * @param {string} searchWord
 * @return {string[][]}
 */
 var  suggestedProducts   = function (products , searchWord ) {
    products .sort ((a , b ) => {
         if  (a   >  b )  return   1
         if  (a   <  b )  return   -  1
         return   0
    })

     const  r   =  []

     for  ( let  i   =   1 ; i  <= searchWord .length ; i  ++ ) {
         const  arr   =  products .filter ((s ) => s .startsWith (searchWord .substring ( 0 , i ))).slice ( 0 ,  3 )
       //console.log(searchWord.substring(0, i))
         r .push (arr )
    }

     return  r
};