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.
Sam and substrings
Sam and substrings
Sort by
recency
|
206 Discussions
|
Please Login in order to post a comment
js
Kotlin solution:
To help understand the above solution, I'm posting a slower solution, where the "frontier" represents the set of all subsets ending at the current "last" character. Note that when encountering a new character, a new frontier is formed by appending the new charater to every element of the old frontier (plus, the lone character itself, set of one character). While doing that, the numerical sum of the old frontier is added to the result. The fast solution transforms the frontier from a set to a number (representing the set's sum). Instead of adding a new character to the end of every element of the frontier, we calculate the new sum directly. You can do that by multiplying the old frontier's sum by 10 (because every digit is shifted left by one position), plus adding the new digit n times (once to each element of the old frontier, plus the lone digit itself, the set of one character).
Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Sam and substrings Problem Solution