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.
I tested it and it's faster than the simple iterative solution. The Counter initialization is a one time task. The part that constructs t and checks if t meets the required criterion is also O(n) and, without checking the counts, it would run for n*(n-1)/2 times (where n = len(s)). Checking the counts before doing all this would avoid that O(n) operation, when unnecessary. The overall running time is therefore less with the Counter based logic.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
I tested it and it's faster than the simple iterative solution. The Counter initialization is a one time task. The part that constructs t and checks if t meets the required criterion is also O(n) and, without checking the counts, it would run for n*(n-1)/2 times (where n = len(s)). Checking the counts before doing all this would avoid that O(n) operation, when unnecessary. The overall running time is therefore less with the Counter based logic.