We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Each iteration takes N units of time because indexOf do sequential search each time you are trying to find some character and you are searching for it 26 times. The overall time is about 26N
A better approach is to store 26 boolean flags for each character of each words and check if their boolean flags intersect, if any of the flag intersect then 2 words are common. This approach only scans through each words once.
Two Strings
You are viewing a single comment's thread. Return to all comments →
Each iteration takes N units of time because indexOf do sequential search each time you are trying to find some character and you are searching for it 26 times. The overall time is about 26N
A better approach is to store 26 boolean flags for each character of each words and check if their boolean flags intersect, if any of the flag intersect then 2 words are common. This approach only scans through each words once.