Facebook Interview question - Need optimal solution
Anonymous User
287

In a birthday party, kids are coming to celebrate birthday. At the door they are giving a scret word (it is a combination of letter and numbers). Now, put all those secret into a magicBox. The magicBox will return an array that contains set of charecters from with we can from any of the secret number.

Example: input : ['abc', 'aabcd', 'def']
output: ['a', 'a', 'b', 'c', 'd', 'e', 'f']
from the output charecter set we can from any of the input words.

```
let getmagicNumber = (s) => {
  const finalMap = new Map();
  for(let word of s){
    let helperMap = new Map();
    for(let i = 0; i < word.length; i++) {
      let curr = word.charAt(i);
      if(helperMap.has(curr)) 
          helperMap.set(curr, helperMap.get(curr) + 1);
      else 
          helperMap.set(curr, 1);
    }
    for(let key of helperMap.keys()) {
      if(finalMap.has(key)) 
          finalMap.set(key, Math.max(finalMap.get(key), helperMap.get(key)));
      else 
          finalMap.set(key, helperMap.get(key));
    }
  }
   let result = [];
  for(let key of finalMap.keys()) {
    let times = finalMap.get(key);
    while(times-- > 0) {
      result.push(key);
    }
  }
  return result;
}
console.log(getmagicNumber(['abc','aabcd', 'def']))
```

It was my solution and I said then both time and space complexity is O(NL) where N is the number of magic words in input an L is the length of maximum latters in a word.

What is wrong with my answer. Please help.
Comments (2)