Minimum Genetic Mutation LeetCode solution in JavaScript
1/**2* @param {string} start3* @param {string} end4* @param {string[]} bank5* @return {number}6*/7var minMutation = function (start, end, bank) {8if (start.length != end.length || !contains(end, bank)) {9return -1;10}11return bfs(start, end, bank);12};13function contains(end, bank) {14for (const x of bank) {15if (x == end) {16return true;17}18}19return false;20}21function convertPossible(a, b) {22if (a.length != b.length) return false;23let count = 0;24for (let i = 0; i < a.length; i++) {25if (a[i] != b[i]) {26count++;27}28}29return count == 1;30}31function bfs(start, end, bank) {32let count = 0;33let q = [];34let set = new Set();35q.push(start);3637while (q.length > 0) {38let size = q.length;3940for (let i = 0; i < size; i++) {41let curr = q.shift();4243if (curr == end) {44return count;45}46bank.forEach((x) => {47if (convertPossible(curr, x) && !set.has(x)) {48q.push(x);49set.add(x);50}51});52}53count++;54}55return -1;56}