Given a string S, I want to find out whether there are non-overlapping substrings A, B and C in S, so that the equation A + B = C holds when the substrings are interpreted as decimal numbers.
Example: For S = 17512, the answer is yes, because 12 + 5 = 17 holds.
This is not a homework question, I have tried approaching this problem building a suffix array
17512
7512
512
12
2
but then I realize that given 132, 1 + 2 = 3 Would require other forms of permutations in selection?
How do you solve this in an efficient way?
We can calculate the sum of digits of a number by adding a number's digits while ignoring the place values. So, if we have the number 567, we can calculate the digit sum as 5 + 6 + 7, which equals 18.
Naive Approach: The simplest approach to solve the given problem is to iterate over all the digits of the given number N and if there exists any distinct digit then print Yes. Otherwise, print No. Below is the implementation of the above approach: C++
In C#, we can use the IsDigit() method to check if a character is numeric or a digit. The IsDigit() method can be used on a single character or on a string. In the case of a string, we have to specify the index position of the character in the string.
How do you check if a number contains a digit in Python? Use str. isdigit() to check if a string contains a number Check if any character is a digit by calling str. isdigit(char) on each character char .
Let S be the decimal representation of the number. If n = |S| is small enough (<500 or so), you can use the following algorithm:
Let us enumerate A and C from the equation A + B = C (where we assume w.l.o.g. A > B). We know that they need to be of around the same size (plus/minus one digit), so enumerating the possibilities is a cubic operation (there are O(n3) candidates).
For every candidate pair (A, C), we need to check whether B = C - A
is in the string and not overlapping with any of the A or C substrings. We can compute the difference in linear time using arithmetics in base 10.
The tricky part is to check whether B is a substring not overlapping A or C. A and C split the string into 3 parts:
S = xAyCz
If we enumerate them in a clever way, with fixed start positions and decreasing size, we can maintain suffix automata of part x and the reverses of parts y and z.
Now we can check in linear time whether B = C - A (or its reverse) exists in one of the three parts.
Time complexity of this approach: Θ(n4).
There is a variation of this which is slightly more complicated, but faster (thanks to Evgeny for pointing it out):
Runtime: O(n3 log n).
UPDATE: regarding the simplified version where all characters need to be used:
We first realize that we can do arithmetics on substrings of our string in linear time, if we work in base 10.
Now we want to find the splitting points a < b, so that your three substrings are A = s1...sa, B = sa+1...sb and C = sb+1...sn.
We can prove that there is only a constant number of candidates for a and b, because all three parts must have approximately the same size for the equation to hold.
Using arbitrary precision arithmetics, we can easily try out all candidate pairs (a,b) and for each of those, find M = max(A,B,C). Then just check whether M is the sum of the other two numbers.
Total time complexity: Θ(n).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With