Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import deque, defaultdict

class Solution:
def minMutation(self, startGene: str, endGene: str, bank: List[str]) -> int:
"""
Find minimum number of mutations from startGene to endGene.
Uses BFS to find shortest path. Each mutation changes one character,
and the resulting gene must be in the bank.

Time Complexity: O(N * L * 4) where N is bank size, L is gene length (8)
Space Complexity: O(N) for visited set and queue
"""
# If endGene is not in bank, it's impossible
if endGene not in bank:
return -1

# Convert bank to set for O(1) lookup
bank_set = set(bank)

# BFS queue: (current_gene, mutation_count)
queue = deque([(startGene, 0)])
visited = {startGene}

# Possible gene characters
genes = ['A', 'C', 'G', 'T']

while queue:
current_gene, mutations = queue.popleft()

# If we reached the end gene, return the mutation count
if current_gene == endGene:
return mutations

# Try all possible single character mutations
for i in range(len(current_gene)):
for gene_char in genes:
# Skip if same character
if gene_char == current_gene[i]:
continue

# Create mutated gene
mutated = current_gene[:i] + gene_char + current_gene[i+1:]

# If mutation is valid and not visited
if mutated in bank_set and mutated not in visited:
visited.add(mutated)
queue.append((mutated, mutations + 1))

# No path found
return -1
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,4 @@ function snakesAndLadders(board: number[][]): number {

// If we can't reach the target
return -1;
}

// Test cases
console.log("Example 1:");
const board1 = [
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,35,-1,-1,13,-1],
[-1,-1,-1,-1,-1,-1],
[-1,15,-1,-1,-1,-1]
];
console.log(`Input: board = ${JSON.stringify(board1)}`);
console.log(`Output: ${snakesAndLadders(board1)}`); // Expected: 4
console.log(`Explanation: In the beginning, you start at square 1 (at row 5, column 0).
You decide to move to square 2 and must take the ladder to square 15.
You then decide to move to square 17 and must take the snake to square 13.
You then decide to move to square 14 and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
This is the lowest possible number of moves to reach the last square, so return 4.`);

console.log("\nExample 2:");
const board2 = [[-1,-1],[-1,3]];
console.log(`Input: board = ${JSON.stringify(board2)}`);
console.log(`Output: ${snakesAndLadders(board2)}`); // Expected: 1

export { snakesAndLadders };

}
Loading