Given 2 strings find the common words along with the time and space complexity. How would you optimize the algorithm
Anonymous
To get O(N) for time complexity, you can split each string by spaces, traverse the first string and put each word into a hash, then traverse the second string of words checking each word to see if it was in the hash. Hash functions are O(1) and traversing each string is O(N), so, final time complexity is O(N).
Check out your Company Bowl for anonymous work chats.