We're a place where coders share, stay up-to-date and grow their careers.
What do you think about this solution?
const canScrabbleString = (word, letters) => { const map = new Map(); [...letters].map(char => { const count = map.get(char) ?? 0; map.set(char, count + 1); }); for (let i = 0; i < word.length; ++i) { const char = word[i]; const count = map.get(char) ?? 0 if (count <= 0) return false; map.set(char, count - 1); }; return true; }; const lettersInHand = "heulselo"; const wordToBeFound = "hello"; console.log( canScrabbleString(wordToBeFound, lettersInHand) );
I hope it is clear and it's time complexity is O(w + l) where w = word length and l = letters length
O(w + l)
w = word length
l = letters length
You can play with this code here:
instacode scratchpad
What do you think about this solution?
I hope it is clear and it's time complexity is
O(w + l)
wherew = word length
andl = letters length
You can play with this code here:
instacode scratchpad