classSolution{publicList<List<String>>suggestedProducts(String[] products,String searchWord){// Base case// Sort the products lexicographicallyArrays.sort(products);List<List<String>> res =newArrayList<List<String>>();int l =0, r = products.length -1;// Match the wordfor(int i =0; i < searchWord.length();++i){char c = searchWord.charAt(i);// When index exceeds products' item length or it doesn't match with the characterwhile(l <= r &&(products[l].length()<= i || products[l].charAt(i)!= c)){++l;}while(l <= r &&(products[r].length()<= i || products[r].charAt(i)!= c)){--r;}int remainWindowLen = r - l +1;List<String> tempList =newArrayList<>();// Add the windowfor(int j =0; j <Math.min(3, remainWindowLen);++j){
tempList.add(products[l + j]);}// Add to the result
res.add(tempList);}return res;}}