【Freedom Trail】

In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring” and use the dial to spell a specific keyword to open the door.

Given a string ring that represents the code engraved on the outer ring and another string key that represents the keyword that needs to be spelled, return the minimum number of steps to spell all the characters in the keyword.

Initially, the first character of the ring is aligned at the “12:00” direction. You should spell all the characters in key one by one by rotating ring clockwise or anticlockwise to make each character of the string key aligned at the “12:00” direction and then by pressing the center button.

At the stage of rotating the ring to spell the key character key[i]:

  1. You can rotate the ring clockwise or anticlockwise by one place, which counts as one step. The final purpose of the rotation is to
    align one of ring’s characters at the “12:00” direction, where this character must equal key[i].
  2. If the character key[i] has been aligned at the “12:00” direction, press the center button to spell, which also counts as one step.
    After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.

Example 1:

在这里插入图片描述

Input: ring = "godding", key = "gd"
Output: 4
Explanation:
For the first key character 'g', since it is already in place, we just need 1 step to spell this character. 
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.

Example 2:

Input: ring = "godding", key = "godding"
Output: 13

Constraints:

  • 1 <= ring.length, key.length <= 100
  • ring and key consist of only lower case English letters.
  • It is guaranteed that key could always be spelled by rotating ring.
    Java Completion:
/**
 * 代码模拟拨号盘实现
 * 1。ring首表示12点钟的位置,ring表示顺时针遍历的字符
 * 2。index表示匹配key的下标
 * 3。for循环表示一次拨号完成
 * note:因为是递归实现,key长度可能很长,所以增加map来优化递归的操作
 */
class Solution {
    public int findRotateSteps(String ring, String key) {
        Map<String, Integer> map = new HashMap<>();
        return dfs(ring, key, 0, map);
    }

    public int dfs(String ring, String key, int index, Map<String, Integer> map) {
        if (index == key.length()) return 0;
        String hashKey = ring + index;
        if (map.containsKey(hashKey)) return map.get(hashKey);
        int minStep = Integer.MAX_VALUE;
        char c = key.charAt(index);
        int firstIndex = ring.indexOf(c);
        int lastIndex = ring.lastIndexOf(c);

        for (int i = 0; i < ring.length(); i++) {
            if (i == firstIndex || i == lastIndex) {
                String s = ring.substring(i, ring.length()) + ring.substring(0, i);
                int step = 1 + Math.min(i, ring.length() - i);
                step += dfs(s, key, index + 1, map);
                minStep = Math.min(minStep, step);
            }
        }
        map.put(hashKey, minStep);
        return minStep;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值