Guest User

Untitled

a guest
Jan 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. class Solution {
  2. public int findRotateSteps(String ring, String key) {
  3. // Map: (ringIndex, keyIndex) -> steps
  4. Map<MemoKey, Integer> memo = new HashMap<>();
  5. return helper(ring, key, 0, 0, memo);
  6. }
  7.  
  8. // ringIndex: specifies which index of ring is at 12:00.
  9. // keyIndex: specifies the index of the next letter we need.
  10. public int helper(String ring, String key, int ringIndex, int keyIndex,
  11. Map<MemoKey, Integer> memo) {
  12. if (keyIndex == key.length()) {
  13. return 0;
  14. }
  15.  
  16. MemoKey memoKey = new MemoKey(ringIndex, keyIndex);
  17. if (memo.containsKey(memoKey)) {
  18. return memo.get(memoKey);
  19. }
  20.  
  21. char nextChar = key.charAt(keyIndex);
  22.  
  23. // Find two indices of nextChar that are closest to us.
  24. int nextLeft = getNextLeft(ring, ringIndex, nextChar);
  25. int nextRight = getNextRight(ring, ringIndex, nextChar);
  26.  
  27. // Find how many steps to get to nextLeft and nextRight.
  28. int stepsLeft = getStepsLeft(ringIndex, nextLeft, ring.length());
  29. int stepsRight = getStepsRight(ringIndex, nextRight, ring.length());
  30.  
  31.  
  32. //System.out.printf("Going from %d to %s left (idx %d). %d steps.\n",
  33. // ringIndex, nextChar, nextLeft, stepsLeft);
  34.  
  35.  
  36. // Recurse going left and right and return the min.
  37. int futureStepsLeft = helper(ring, key, nextLeft, keyIndex + 1, memo);
  38. int futureStepsRight = helper(ring, key, nextRight, keyIndex + 1, memo);
  39.  
  40. // Return the min of going left or right.
  41. // Add 1 because spelling a letter takes a step.
  42. int totalStepsLeft = stepsLeft + futureStepsLeft;
  43. int totalStepsRight = stepsRight + futureStepsRight;
  44.  
  45. int result = 1 + Math.min(totalStepsLeft, totalStepsRight);
  46. memo.put(memoKey, result);
  47. return result;
  48. }
  49.  
  50. private static class MemoKey {
  51. private int ringIdx;
  52. private int keyIdx;
  53.  
  54. MemoKey(int ringIdx, int keyIdx) {
  55. this.ringIdx = ringIdx;
  56. this.keyIdx = keyIdx;
  57. }
  58.  
  59. @Override
  60. public int hashCode() {
  61. return Objects.hash(ringIdx, keyIdx);
  62. }
  63.  
  64. @Override
  65. public boolean equals(Object other) {
  66. if (other instanceof MemoKey) {
  67. MemoKey otherMK = (MemoKey) other;
  68. return this.ringIdx == otherMK.ringIdx
  69. && this.keyIdx == otherMK.keyIdx;
  70. }
  71. return false;
  72. }
  73. }
  74.  
  75. private int getNextLeft(String ring, int ringIndex, char nextChar) {
  76. return getNext(ring, ringIndex, nextChar, -1);
  77. }
  78.  
  79. private int getNextRight(String ring, int ringIndex, char nextChar) {
  80. return getNext(ring, ringIndex, nextChar, 1);
  81. }
  82.  
  83. private int getNext(String ring, int ringIndex, char nextChar, int increment) {
  84. int curr = ringIndex;
  85. while (ring.charAt(curr) != nextChar) {
  86. curr = (curr + increment + ring.length()) % ring.length();
  87. }
  88. return curr;
  89. }
  90.  
  91. private int getStepsLeft(int ringIndex, int nextLeft, int ringLen) {
  92. if (nextLeft <= ringIndex) {
  93. return ringIndex - nextLeft;
  94. }
  95. return ringIndex + ringLen - nextLeft;
  96. }
  97.  
  98. private int getStepsRight(int ringIndex, int nextRight, int ringLen) {
  99. if (nextRight >= ringIndex) {
  100. return nextRight - ringIndex;
  101. }
  102. return nextRight + ringLen - ringIndex;
  103. }
  104.  
  105.  
  106. }
Add Comment
Please, Sign In to add comment