ARTICLEViews: 36Share  Posted by - Anonymous

Given an array of words and a number k, your task is to return an array with length k

Given an array of words and a number k, your task is to return an array with length k where the ith element is the number of unique prefixes with length i + 1 among the given words (including only words that are at most i characters long).



function solution(words, k) {

  const rtnArr = new Array(k);

  const prfArr = [];

   

  for(let i = 0; i < k; i++){

    const prArr = words.map(item => item.length >= i+1 ? item.substring(0, i+1) : "");

    const uniqueArr = prArr.filter((value, index, self) => {

    return value && self.indexOf(value) === index;

  });

    rtnArr[i] = uniqueArr.length;

  }

  return rtnArr;

}


Auto Suggestions & Related Videos -


arrays #coding #programming #competitiveprogramming #coding #dsa Hey, Guys in this video I have explained how we can ...

Reverse an Array in Groups of Given Size

GOOGLE CODING INTERVIEW QUESTION - KTH LARGEST ELEMENT IN AN ARRAY (LeetCode)

Find The Index Of K In A Sorted Array OR Where It Belongs To Keep It Sorted | C Programming Example

Lecture 10: Solving LeetCode/CodeStudio Questions [Arrays]

Views -