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.
If you understand the main idea behind this problem, then the solution is easy.
We have to figure out two things:
The correct starting number (first_num)
Whether the string forms a beautiful sequence from that starting number.
Algo:
Iterate through the string and try every possible starting number by taking its prefix.
*foriinrange(1,len(s)// 2 + 1): # Try every possible length for firstnumfirstnum=int(s[:i])#Guessthefirstnumbernextnum=firstnum+1#Thenextexpectednumberinsequencetemp=s[:i]#Startbuildingthesequencefromfirstnum*
Now simulate building the sequence by adding next numbers:
Separate the Numbers
You are viewing a single comment's thread. Return to all comments →
If you understand the main idea behind this problem, then the solution is easy. We have to figure out two things:
Algo:
Iterate through the string and try every possible starting number by taking its prefix.
Now simulate building the sequence by adding next numbers:
Finally, check if the built string matches the original:
If no such starting number worked, then the string isn’t beautiful:
print("NO")