JavaScript Dynamic Programming & Dijkstra's (2 solutions)
Dynamic Programming (fastest)\njavascript\n/**\n * @param {string} word\n * @return {number}\n */\nconst map = {\n  A: [0,0],\n  B: [0,1],\n  C: [0,2],\n  D: [0
# Dynamic Programming (fastest)\n```javascript\n/**\n * @param {string} word\n * @return {number}\n */\nconst map = {\n  A: [0,0],\n  B: [0,1],\n  C: [0,2],\n  D: [0,3],\n  E: [0,4],\n  F: [0,5],\n  G: [1,0],\n  H: [1,1],\n  I: [1,2],\n  J: [1,3],\n  K: [1,4],\n  L: [1,5],\n  M: [2,0],\n  N: [2,1],\n  O: [2,2],\n  P: [2,3],\n  Q: [2,4],\n  R: [2,5],\n  S: [3,0],\n  T: [3,1],\n  U: [3,2],\n  V: [3,3],\n  W: [3,4],\n  X: [3,5],\n  Y: [4,0],\n  Z: [4,1],\n}\n\nconst distance = (ltr1, ltr2) => (\n  Math.abs(map[ltr1][0] - map[ltr2][0]) + Math.abs(map[ltr1][1] - map[ltr2][1])\n)\n\n/**\n * @param {string} word\n * @return {number}\n */\nvar minimumDistance = function(word) {\n  const chars = new Set(word.split(\'\'))\n  const cache = new Array(word.length);\n  for (let i = 0; i < cache.length; i++) cache[i] = {};\n  const dfs = (left, right, next) => {\n    if (next === word.length) return 0;\n    const key = left === word[next - 1] ? right : left;\n    if (cache[next][key] !== undefined) return cache[next][key];\n    let res = dfs(word[next], right, next + 1) + distance(left, word[next]);\n    res = Math.min(res, dfs(left, word[next], next + 1) + distance(right, word[next]));\n    return cache[next][key] = res;\n  }\n  let min = Infinity;\n  for (let char of chars) {\n    min = Math.min(min, dfs(word[0], char, 1))\n  }\n  return min;\n};\n```\n\n# Dijkstra\'s Algorithm (works)\n\n```javascript\nclass MinHeap {\n  constructor() {\n    this.store = [];\n  }\n\n  isEmpty() {\n    return this.store.length === 0;\n  }\n\n  pop() {\n    if (this.store.length < 2) return this.store.pop();\n    const result = this.store[0];\n    this.store[0] = this.store.pop();\n    this.heapifyDown(0);\n    return result;\n  }\n\n  push(value) {\n    this.store.push(value);\n    this.heapifyUp(this.store.length - 1);\n  }\n\n  heapifyDown(parent) {\n    const childs = [1,2].map((n) => parent * 2 + n).filter((n) => n < this.store.length);\n    let child = childs[0];\n    if (childs[1] && this.store[childs[1]].distance < this.store[child].distance) {\n      child = childs[1];\n    }\n    if (child && this.store[child].distance < this.store[parent].distance) {\n      const temp = this.store[child];\n      this.store[child] = this.store[parent];\n      this.store[parent] = temp;\n      this.heapifyDown(child);\n    }\n  }\n\n  heapifyUp(child) {\n    const parent = Math.floor((child - 1) / 2);\n    if (child && this.store[child].distance < this.store[parent].distance) {\n      const temp = this.store[child];\n      this.store[child] = this.store[parent];\n      this.store[parent] = temp;\n      this.heapifyUp(parent);\n    }\n  }\n}\n\n/**\n * @param {string} word\n * @return {number}\n */\nconst map = {\n  A: [0,0],\n  B: [0,1],\n  C: [0,2],\n  D: [0,3],\n  E: [0,4],\n  F: [0,5],\n  G: [1,0],\n  H: [1,1],\n  I: [1,2],\n  J: [1,3],\n  K: [1,4],\n  L: [1,5],\n  M: [2,0],\n  N: [2,1],\n  O: [2,2],\n  P: [2,3],\n  Q: [2,4],\n  R: [2,5],\n  S: [3,0],\n  T: [3,1],\n  U: [3,2],\n  V: [3,3],\n  W: [3,4],\n  X: [3,5],\n  Y: [4,0],\n  Z: [4,1],\n}\n\nconst getDistance = (ltr1, ltr2) => (\n  Math.abs(map[ltr1][0] - map[ltr2][0]) + Math.abs(map[ltr1][1] - map[ltr2][1])\n)\n\n/**\n * @param {string} word\n * @return {number}\n */\nvar minimumDistance = function(word) {\n  const heap = new MinHeap();\n  for (let letter in map) {\n    heap.push({ left: word[0], right: letter, pos: 1, distance: 0 });\n  }\n  const visited = word.split(\'\').map(() => new Set());\n  while (!heap.isEmpty()) {\n    const { left, right, pos, distance } = heap.pop();\n    if (pos === word.length) return distance;\n    const key = left === word[pos - 1] ? right : left;\n    if (visited[pos].has(key)) continue;\n    visited[pos].add(key)\n    heap.push({\n      left: word[pos],\n      right,\n      pos: pos + 1,\n      distance: distance + getDistance(left, word[pos])\n    })\n    heap.push({\n      left,\n      right: word[pos],\n      pos: pos + 1,\n      distance: distance + getDistance(right, word[pos])\n    })\n  }\n};\n```
['Dynamic Programming', 'JavaScript']