Ad
  • Custom User Avatar

    This is more of a DP approach than memoization, which the author particular asked for in the prompt.
    If doing DP, there is no need for the list data structure. You can just keep 2 variables for n-1 and n-2. This reduces Space complexity to O(1)

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    In addition, sorting takes nlogn time, where n is the number of characters. First, you can escape right away if the number of characters in item is not equal to the number of characters in word. Second, checking with histogram rather than sorting significantly improves run time, making it O(n).