You are viewing a single comment's thread. Return to all comments →
If you want to utilize the Counter you can do (Counter(s1) - Counter(s2)).elements() this returns an iterator. So either convert to list then sum or use sum(1 for _ in iterator).
You can also do (Counter(s1) - Counter(s2)).values() to get the list of counts outright. Not a big difference but at least you can skip the step of converting to a list :)
Yep. A simple
print(sum((Counter(s[:l//2]) - Counter(s[l//2:])).values()))
works (after checking the length is even of course). No loop!
can u please explain the use of counter?
Take a look at the C example below by "etayluz", followed by my explanation on why the counter is reduced by half at the end. It might help shed some light as to why a counter is used to solve the problem.