Sam and substrings
Solutions The brute-force approach to solve this problems involves generating all the possible substrings of the input number. This requires \(O(N^2)\) time, which becomes too expensive in the worst case when the input size can be as large as \(2 \cdot 10^5\).
To find an efficient solution, let’s consider the input 8314.
All the possible substrings are listed below:
1 2 3 4 8 3 83 1 31 831 4 14 314 8314 Each line contains all the substrings ending with the corresponding digit.
[Read More]