-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_072.py
70 lines (53 loc) · 2.03 KB
/
problem_072.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class GraphPath:
def __init__(self, nodes=set(), letter_counts=dict()):
self.nodes = nodes
self.letter_counts = letter_counts
def __repr__(self):
return "nodes={}, letters={}".format(self.nodes, self.letter_counts)
def get_max_value_string(graph_path, node, adjacency_map):
if node in graph_path.nodes:
return [graph_path]
new_nodes = graph_path.nodes.copy()
new_nodes.add(node)
new_letter_counts = graph_path.letter_counts.copy()
if node[0] not in new_letter_counts:
new_letter_counts[node[0]] = 0
new_letter_counts[node[0]] += 1
new_graph_path = GraphPath(new_nodes, new_letter_counts)
if node not in adjacency_map:
return [new_graph_path]
paths = list()
for child_node in adjacency_map[node]:
new_paths = get_max_value_string(
new_graph_path, child_node, adjacency_map)
paths.extend(new_paths)
return paths
def get_max_value_string_helper(graph_string, edge_list):
letter_counts = dict()
nodes = list()
for char in graph_string:
if char not in letter_counts:
letter_counts[char] = 0
else:
letter_counts[char] += 1
nodes.append("{}{}".format(char, letter_counts[char]))
adjacency_map = dict()
for start, end in edge_list:
if nodes[start] not in adjacency_map:
adjacency_map[nodes[start]] = set()
if nodes[start] != nodes[end]:
adjacency_map[nodes[start]].add(nodes[end])
paths = list()
graph_path = GraphPath()
for node in adjacency_map:
new_paths = get_max_value_string(graph_path, node, adjacency_map)
paths.extend(new_paths)
max_value = 0
for path in paths:
max_path_value = max(path.letter_counts.values())
if max_path_value > max_value:
max_value = max_path_value
return max_value if max_value > 0 else None
assert get_max_value_string_helper(
"ABACA", [(0, 1), (0, 2), (2, 3), (3, 4)]) == 3
assert not get_max_value_string_helper("A", [(0, 0)])