-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1153.string-transforms-into-another-string.cpp
More file actions
85 lines (82 loc) · 2.25 KB
/
1153.string-transforms-into-another-string.cpp
File metadata and controls
85 lines (82 loc) · 2.25 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Given two strings str1 and str2 of the same length, determine whether you can
// transform str1 into str2 by doing zero or more conversions.
//
// In one conversion you can convert all occurrences of one character in str1 to
// any other lowercase English character.
//
// Return true if and only if you can transform str1 into str2.
//
//
//
// --------------------------------------------------
//
// Example 1:
//
//
// Input: str1 = "aabcc", str2 = "ccdee"
// Output: true
// Explanation: Convert 'c' to 'e' then 'b' to 'd' then 'a' to 'c'. Note that
// the order of conversions matter.
//
//
// --------------------------------------------------
//
// Example 2:
//
//
// Input: str1 = "leetcode", str2 = "codeleet"
// Output: false
// Explanation: There is no way to transform str1 to str2.
//
//
//
//
// * * * * * * * * * * * * * * * * * * * * * * * * *
//
// Note:
//
//
// 1 <= str1.length == str2.length <= 10^4
// Both str1 and str2 contain only lowercase English letters.
//
//
class Solution {
public:
bool canConvert(string str1, string str2) {
if (str1 == str2) {
return true;
}
std::vector<char> graph(26, '\0');
std::vector<int> indegree(26, 0);
for (int i = 0; i < str1.size(); ++i) {
char source = str1[i];
char dest = str2[i];
if (graph[source - 'a'] == '\0' || graph[source - 'a'] == dest) {
// std::cout << "setting " << source << " to " << dest << std::endl;
graph[source - 'a'] = dest;
} else {
// each letter can only be mapped to another one letter
// its a many to one relationship between letters in the graph
// if a single letter
// std::cout << source << " has previous map to " << graph[source]
// << std::endl;
return false;
}
++indegree[dest - 'a'];
}
int zero_indegree_nodes = 0;
int excess_indegree_edges = 0;
for (int i = 0; i < 26; ++i) {
if (!indegree[i]) {
++zero_indegree_nodes;
} else if (indegree[i] > 1) {
excess_indegree_edges += indegree[i] - 1;
}
}
// are there any nodes with no incoming edges that we can use as spare
if (zero_indegree_nodes && excess_indegree_edges <= zero_indegree_nodes) {
return true;
}
return false;
}
};