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.
Instead of adding each char to the set and letting the set look through itself to determine if the char has already been added to it (which may be a O(n) operation), you could just do the check yourself. I did it using an array of booleans of size 26. When I see the character, I set the bool value to true (looking up using the letters ASCII value). This is a O(1) operation. This makes the solution O(n) because we only look through each character one time.
String Construction
You are viewing a single comment's thread. Return to all comments →
@WatchandLearn
Instead of adding each char to the set and letting the set look through itself to determine if the char has already been added to it (which may be a O(n) operation), you could just do the check yourself. I did it using an array of booleans of size 26. When I see the character, I set the bool value to true (looking up using the letters ASCII value). This is a O(1) operation. This makes the solution O(n) because we only look through each character one time.