Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
only the new call increments index. In the original forloop index<arr.length holds true.
Admitedly this code is hard to read.
the class Transition can be reduces to one line with the use of records from Java 14 onwards.
Easier, yes.
Simpler, no: recursion is harder to read, understand and debug.
Faster, barely: log10(n) is only slightly faster than converting to String.
Space efficient, hard no: recursion will alocate more stack space than a string.
Personaly I prefer the solution above more, as it is easier work with(all be at slower).
"If you use an array to store the number of occurances …"
it sound to me like a histogram, and for that a hash map would be a better data structure.
It only says that the digit root is derived by recursivly repeating it sum of digits, however that does not imply that the problem has to be solved recursivly.
Don’t follow principles and rules blindly, understand how to apply them.
When does this principle apply:
Multiple entries: Always, it is only possible in assembler and strongly discouraged from.
Multiple exits: Recursion and long functions, improve readability and avoids unforeseen behaviour.\
Neither of them apply here.
I like the solution as it simple and straight forward, however I think there are some improvemnt to be had nevertheless.
One such improvement would be to check for blank string and return it back.