aboutsummaryrefslogtreecommitdiff
path: root/lexicographically_small_string.cpp
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /lexicographically_small_string.cpp
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'lexicographically_small_string.cpp')
-rw-r--r--lexicographically_small_string.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/lexicographically_small_string.cpp b/lexicographically_small_string.cpp
new file mode 100644
index 0000000..3a58189
--- /dev/null
+++ b/lexicographically_small_string.cpp
@@ -0,0 +1,42 @@
+#include "leetcode.h"
+
+/*
+ * given: 2 strings of same length ('s1', 's2'), & a string 'baseStr'
+ * s1[i] and s2[i] are equivalent characters
+ * eg. s1 = "abc", s2 = "cde", 'a' == 'c', 'b' == 'd', etc.
+ * return lexicographically smallest equivalent string of 'baseStr' by using
+ * equivalency information from 's1' & 's2'
+ */
+
+class Solution {
+public:
+ // union find
+ string smallestEquivalentString(string s1, string s2, string baseStr) {
+ vector<int> ds(26, -1);
+ for (auto i = 0; i < s1.size(); ++i)
+ dsMerge(ds, s1[i] - 'a', s2[i] - 'a');
+ for (auto i = 0; i < baseStr.size(); ++i)
+ baseStr[i] = dsFind(ds, baseStr[i] - 'a') + 'a';
+ return baseStr;
+ }
+
+private:
+ int dsFind(vector<int> &ds, int p) {
+ return ds[p] == -1 ? p : ds[p] = dsFind(ds, ds[p]);
+ }
+ void dsMerge(vector<int> &ds, int p1, int p2) {
+ p1 = dsFind(ds, p1), p2 = dsFind(ds, p2);
+ if (p1 != p2)
+ ds[max(p1, p2)] = min(p1, p2);
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.smallestEquivalentString("parker", "morris",
+ "parser"); // expect: makkek
+ cout << obj.smallestEquivalentString("hello", "world", "hold"); // expect:
+ // hdld
+ cout << obj.smallestEquivalentString("leetcode", "programs",
+ "sourcecode"); // expect: aauaaaaada
+}