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.app
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.app