Ad
  • Custom User Avatar

    only the new call increments index. In the original forloop index<arr.length holds true.
    Admitedly this code is hard to read.

  • Custom User Avatar

    the class Transition can be reduces to one line with the use of records from Java 14 onwards.

  • Custom User Avatar

    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).

  • Custom User Avatar

    "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.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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.