编辑代码

// 设置标准输入接口
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  const str = await readline();
  const target = await readline();

  let res = Infinity;
  const dfs = (path, idx, startIndex) => {
    if (idx == target.length) {
      const sum = Math.min(path.reduce((a, b) => a + b));
      res = Math.min(res, sum);
      return;
    }
    for (let i = 0; i < str.length; i++) {
      if (str[i] == target[idx]) {
        dfs([...path, Math.abs(i - startIndex)], idx + 1, i);
      }
    }
  };
  dfs([], 0, 0);
  console.log(res);
})();