Looks good. But I just wanted to add that your last piece of code, where you do "res += numstrs" could be slow for large inputs. You could make it even faster by using stringstream or reserving space ahead of time.