| 模型 | 模型答案 | 正确答案 |
|---|---|---|
| 金声玉亮2.0 | ```python from typing import List from collections import defaultdict class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a dictionary to store the minimum cost to change from one character to another min_cost = defaultdict(lambda: defaultdict(lambda: float('inf'))) # Populate the dictionary with the given costs for o, c, co in zip(original, changed, cost): min_cost[o][c] = min(min_cost[o][c], co) # Function to find the minimum cost to change from one character to another using Floyd-Warshall algorithm def floyd_warshall(): for k in min_cost: for i in min_cost: for j in min_cost: if min_cost[i][k] != float('inf') and min_cost[k][j] != float('inf'): min_cost[i][j] = min(min_cost[i][j], min_cost[i][k] + min_cost[k][j]) # Apply Floyd-Warshall algorithm to find the minimum cost between all pairs of characters floyd_warshall() # Calculate the total minimum cost to convert source to target total_cost = 0 for s, t in zip(source, target): if s != t: if min_cost[s][t] == float('inf'): return -1 total_cost += min_cost[s][t] return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| deepseek-r1 | ```python class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: INF = 1 << 60 # A very large number to represent infinity dist = [[INF] * 26 for _ in range(26)] # Initialize diagonal (same character) to 0 cost for i in range(26): dist[i][i] = 0 # Fill the distance matrix with direct edges for o, c, co in zip(original, changed, cost): u = ord(o) - ord('a') v = ord(c) - ord('a') if co < dist[u][v]: dist[u][v] = co # Floyd-Warshall algorithm to compute all pairs shortest paths for k in range(26): for i in range(26): for j in range(26): if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j] total = 0 for s, t in zip(source, target): if s == t: continue u = ord(s) - ord('a') v = ord(t) - ord('a') if dist[u][v] == INF: return -1 total += dist[u][v] return total ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| gpto1 | ```python class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: INF = 1 << 60 n = len(source) source = source target = target # Initialize cost matrix cost_matrix = [[INF] * 26 for _ in range(26)] for i in range(26): cost_matrix[i][i] = 0 # Cost to convert a letter to itself is 0 # Build the direct transformation cost for o, c, z in zip(original , changed, cost): x = ord(o) - ord('a') y = ord(c) - ord('a') if cost_matrix[x][y] > z: cost_matrix[x][y] = z # Keep minimal cost # Floyd-Warshall algorithm to find minimal cost between all pairs for k in range(26): for i in range(26): for j in range(26): if cost_matrix[i][k] + cost_matrix[k][j] < cost_matrix[i][j]: cost_matrix[i][j] = cost_matrix[i][k] + cost_matrix[k][j] total_cost = 0 for s_char, t_char in zip(source, target): x = ord(s_char) - ord('a') y = ord(t_char) - ord('a') if cost_matrix[x][y] == INF: return -1 # Impossible to transform s_char to t_char else: total_cost += cost_matrix[x][y] return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| gpt-4o-2024-08-06 | ```python from typing import List from collections import defaultdict import heapq class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a graph of transformations with costs graph = defaultdict(list) for o, c, z in zip(original, changed, cost): graph[o].append((c, z)) # Function to find the minimum cost to transform a single character def dijkstra(start, end): if start == end: return 0 # Priority queue for Dijkstra's algorithm pq = [(0, start)] # Visited set to keep track of minimum cost to reach each character visited = {} while pq: current_cost, current_char = heapq.heappop(pq) if current_char in visited and visited[current_char] <= current_cost: continue visited[current_char] = current_cost if current_char == end: return current_cost for neighbor, edge_cost in graph[current_char]: if neighbor not in visited or visited[neighbor] > current_cost + edge_cost: heapq.heappush(pq, (current_cost + edge_cost, neighbor)) return float('inf') total_cost = 0 for s_char, t_char in zip(source, target): cost_to_transform = dijkstra(s_char, t_char) if cost_to_transform == float('inf'): return -1 total_cost += cost_to_transform return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| DeepSeek-Coder-V2-Lite-Instruct | ```python class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a dictionary to map original and changed characters to their costs cost_map = {} for i in range(len(original)): if original[i] not in cost_map: cost_map[original[i]] = {} cost_map[original[i]][changed[i]] = cost[i] # Initialize the minimum cost to a large number min_cost = float('inf') # Use dynamic programming to find the minimum cost n = len(source) dp = [[float('inf')] * (n + 1) for _ in range(n + 1)] dp[0][0] = 0 for i in range(n): for j in range(n): # If the characters match, no cost is added if source[i] == target[j]: dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]) # Try to change the character in source to match the character in target if source[i] in cost_map and target[j] in cost_map[source[i]]: dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + cost_map[source[i]][target[j]]) # Skip the character in source dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i + 1][j]) # Skip the character in target dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j + 1]) # The result is the minimum cost to convert the entire source to target min_cost = dp[n][n] return min_cost if min_cost != float('inf') else -1 ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| Meta-Llama-3.1-405B-Instruct-Turbo | ```python from typing import List import heapq class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: graph = {} for orig, ch, c in zip(original, changed, cost): if orig not in graph: graph[orig] = [] graph[orig].append((ch, c)) for char in 'abcdefghijklmnopqrstuvwxyz': if char not in graph: graph[char] = [] dist = {char: float('inf') for char in 'abcdefghijklmnopqrstuvwxyz'} pq = [(0, source[0])] dist[source[0]] = 0 while pq: curr_dist, curr_char = heapq.heappop(pq) if curr_dist > dist[curr_char]: continue for neighbor, weight in graph[curr_char]: if curr_dist + weight < dist[neighbor]: dist[neighbor] = curr_dist + weight heapq.heappush(pq, (dist[neighbor], neighbor)) total_cost = 0 for s, t in zip(source, target): if s == t: continue if dist[t] == float('inf'): return -1 total_cost += dist[t] return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| Meta-Llama-3.1-70B-Instruct-Turbo | ```python from collections import defaultdict, deque from typing import List class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: graph = defaultdict(dict) for orig, chg, cst in zip(original, changed, cost): graph[orig][chg] = min(graph[orig].get(chg, float('inf')), cst) def dijkstra(start): dist = {start: 0} queue = deque([start]) while queue: node = queue.popleft() for neighbor, weight in graph[node].items(): if neighbor not in dist or dist[neighbor] > dist[node] + weight: dist[neighbor] = dist[node] + weight queue.append(neighbor) return dist dist_from_source = {} for char in set(source): dist_from_source[char] = dijkstra(char) total_cost = 0 for src, tgt in zip(source, target): if src == tgt: continue if tgt not in dist_from_source[src]: return -1 total_cost += dist_from_source[src][tgt] return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| Qwen2-72B-Instruct | ```python from collections import defaultdict import heapq class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: graph = defaultdict(lambda: defaultdict(lambda: float('inf'))) for o, c, co in zip(original, changed, cost): graph[o][c] = min(graph[o][c], co) # add direct conversion costs for i in range(len(source)): if source[i] != target[i]: graph[source[i]][target[i]] = min(graph[source[i]][target[i]], abs(ord(source[i]) - ord(target[i]))) # Floyd-Warshall algorithm to find shortest paths between all pairs of characters chars = set(original + changed) dist = {c: {c2: graph[c][c2] for c2 in chars} for c in chars} for k in chars: for i in chars: for j in chars: if dist[i][j] > dist[i][k] + dist[k][j]: dist[i][j] = dist[i][k] + dist[k][j] ans = 0 for i in range(len(source)): if source[i] != target[i]: if dist[source[i]][target[i]] == float('inf'): return -1 ans += dist[source[i]][target[i]] return ans ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| baichuan4 | ```python from collections import defaultdict class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a mapping from each original character to its corresponding changed character and cost char_map = {original[i]: (changed[i], cost[i]) for i in range(len(original))} # Initialize the minimum cost to 0 min_cost = 0 # Iterate through the characters of the source string for i in range(len(source)): # Check if the current character in source needs to be changed if source[i] != target[i]: # Find the minimum cost to change the current character to the target character min_change_cost = float('inf') for orig, (chg, cst) in char_map.items(): if chg == target[i]: min_change_cost = min(min_change_cost, cst) # If it's impossible to change the character, return -1 if min_change_cost == float('inf'): return -1 # Add the minimum cost to the total cost min_cost += min_change_cost return min_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| doubao | ```python class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: n = len(source) dp = [float('inf')] * n dp[0] = 0 if source[0] == target[0] else -1 for i in range(1, n): if source[i] == target[i]: dp[i] = dp[i - 1] else: for j in range(len(original)): if original[j] == source[i] and changed[j] == target[i]: if dp[i - 1]!= -1: dp[i] = min(dp[i], dp[i - 1] + cost[j]) return dp[n - 1] ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| gpto1 | ```python class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: INF = 1 << 60 n = len(source) source = source target = target # Initialize cost matrix cost_matrix = [[INF] * 26 for _ in range(26)] for i in range(26): cost_matrix[i][i] = 0 # Cost to convert a letter to itself is 0 # Build the direct transformation cost for o, c, z in zip(original , changed, cost): x = ord(o) - ord('a') y = ord(c) - ord('a') if cost_matrix[x][y] > z: cost_matrix[x][y] = z # Keep minimal cost # Floyd-Warshall algorithm to find minimal cost between all pairs for k in range(26): for i in range(26): for j in range(26): if cost_matrix[i][k] + cost_matrix[k][j] < cost_matrix[i][j]: cost_matrix[i][j] = cost_matrix[i][k] + cost_matrix[k][j] total_cost = 0 for s_char, t_char in zip(source, target): x = ord(s_char) - ord('a') y = ord(t_char) - ord('a') if cost_matrix[x][y] == INF: return -1 # Impossible to transform s_char to t_char else: total_cost += cost_matrix[x][y] return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| kimi | ```python from typing import List class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a dictionary to store the cost of changing a character to another character change_cost = {} for o, c, co in zip(original, changed, cost): if o not in change_cost: change_cost[o] = {} change_cost[o][c] = co # Initialize the total cost and the previous character total_cost = 0 prev_char = None # Iterate over the characters in the source and target strings for s, t in zip(source, target): # If the characters are the same, skip this iteration if s == t: prev_char = s continue # If the previous character is the same as the current character in the source string, # it means we need to change the character back to the previous character before changing it to the target character if prev_char == s: # If there is no cost for changing the character to the previous character, return -1 if prev_char not in change_cost or t not in change_cost[prev_char]: return -1 total_cost += change_cost[prev_char][t] else: # If there is no cost for changing the character to the target character, return -1 if s not in change_cost or t not in change_cost[s]: return -1 total_cost += change_cost[s][t] prev_char = s return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| minimax | ```python from typing import List class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a dictionary to map each original character to its possible changes and costs change_map = {} for orig, chng, c in zip(original, changed, cost): if orig not in change_map: change_map[orig] = {} change_map[orig][chng] = c # Initialize the cost to 0 total_cost = 0 # Iterate through each character in source and target for s, t in zip(source, target): if s == t: # If the characters are the same, no cost is incurred continue elif s in change_map and t in change_map[s]: # If the character can be changed directly, add the cost total_cost += change_map[s][t] else: # If the character cannot be changed to the target character, return -1 return -1 return total_cost # Example usage: # sol = Solution() # print(sol.minimumCost("abcd", "ac |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| wenxinyiyan | ```python from typing import List import sys class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: n = len(source) INF = sys.maxsize # Create a mapping from (original, changed) pairs to their costs cost_map = {} for orig, chg, cst in zip(original, changed, cost): if (orig, chg) not in cost_map: cost_map[(orig, chg)] = cst # Keep the minimum cost if there are multiple entries for the same pair cost_map[(orig, chg)] = min(cost_map[(orig, chg)], cst) # Create a 2D array to store the minimum costs of converting source[0:i] to target[0:j] dp = [[INF] * 26 for _ in range(n + 1)] # Base case: converting an empty string to an empty string has a cost of 0 dp[0][ord(source[0]) - ord('a')] = 0 # Fill the dp array for i in range(1, n + 1): for j in range(26): # Cost of not changing the character if j == ord(source[i - 1]) - ord('a'): dp[i][j] = dp[i - 1][j] # Cost of changing the character for k in range(26): if k != j and dp[i - 1][k] != INF: key = (chr(k + ord('a')), chr(j + ord('a'))) if key in cost_map: dp[i][j] = min(dp[i][j], dp[i - 1][k] + cost_map[key]) # Check if it's possible to convert source to target if dp[n][ord(target[-1]) - ord('a')] == INF: return -1 return dp[n][ord(target[-1]) - ord('a')] # Note: The provided solution has a time complexity of O(n * 26^2), which may not be efficient for the given constraints. # However, due to the limited number of possible (original, changed) pairs (up to 2000), the solution should work in practice. # For an optimal solution, consider using a graph-based approach with shortest path algorithms like Dijkstra or Bellman-Ford. ``` **Important Note**: The provided solution has a potentially high time complexity and may not be the most efficient approach for the given constraints. In practice, due to the limited number of possible `(original, changed)` pairs (up to 2000), the solution should work. However, for an optimal solution, you may need to consider a graph-based approach using shortest path algorithms like Dijkstra or Bellman-Ford. The idea behind the graph-based approach is to represent each character in the `source` string as a node in the graph. Edges between nodes would represent the possible character transitions and their costs. You would then find the shortest path from the starting node (representing the first character in `source`) to the ending node (representing the last character in `target`). This approach can significantly reduce the time complexity, especially when dealing with large strings and many possible character transitions. |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| zhipu | ```python from typing import List import sys class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a graph where each node is a character and edges represent the cost of changing one character to another graph = {chr(i): {chr(i): 0 for i in range(ord('a'), ord('z') + 1)} for i in range(ord('a'), ord('z') + 1)} # Populate the graph with the given transformations and costs for o, c, co in zip(original, changed, cost): if c in graph[o]: graph[o][c] = min(graph[o][c], co) else: graph[o][c] = co # Use Floyd-Warshall algorithm to find the minimum cost between any two characters for k in graph: for i in graph: for j in graph: if graph[i][j] > graph[i][k] + graph[k][j]: graph[i][j] = graph[i][k] + graph[k][j] # Calculate the minimum cost to convert source to target total_cost = 0 for s_char, t_char in zip(source, target): if s_char == t_char: continue if t_char not in graph[s_char]: return -1 total_cost += graph[s_char][t_char] return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |
| deepseek-v3 | ```python import sys from collections import defaultdict class Solution: def minimumCost(self, source: str, target: str, original: List[str], changed: List[str], cost: List[int]) -> int: # Create a graph to store the minimum cost between any two characters graph = defaultdict(lambda: defaultdict(lambda: sys.maxsize)) # Initialize the graph with direct costs for o, c, cost_val in zip(original, changed, cost): graph[o][c] = min(graph[o][c], cost_val) # Floyd-Warshall algorithm to compute the shortest paths between all pairs chars = set() for o in original: chars.add(o) for c in changed: chars.add(c) chars = list(chars) for k in chars: for i in chars: for j in chars: if graph[i][k] + graph[k][j] < graph[i][j]: graph[i][j] = graph[i][k] + graph[k][j] total_cost = 0 for s, t in zip(source, target): if s == t: continue if graph[s][t] == sys.maxsize: return -1 total_cost += graph[s][t] return total_cost ``` |
[{"input": "\"abcd\"\n\"acbe\"\n[\"a\", \"b\", \"c\", \"c\", \"e\", \"d\"]\n[\"b\", \"c\", \"b\", \"e\", \"b\", \"e\"]\n[2, 5, 5, 1, 2, 20]", "output": "28", "testtype": "functional"}, {"input": "\"aaaa\"\n\"bbbb\"\n[\"a\", \"c\"]\n[\"c\", \"b\"]\n[1, 2]", "output": "12", "testtype": "functional"}, {"input": "\"abcd\"\n\"abce\"\n[\"a\"]\n[\"e\"]\n[10000]", "output": "-1", "testtype": "functional"}] |