From 8c0ffa75673cf586802ebb356f954e4b1beb0f45 Mon Sep 17 00:00:00 2001 From: eat-swap Date: Thu, 28 Apr 2022 00:11:39 +0800 Subject: [PATCH] add: 220427 [cpp] --- cpp/2204/220427.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cpp/2204/220427.cpp diff --git a/cpp/2204/220427.cpp b/cpp/2204/220427.cpp new file mode 100644 index 0000000..49bf9eb --- /dev/null +++ b/cpp/2204/220427.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +/** + * 1202. Smallest String With Swaps + * You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string. + * You can swap the characters at any pair of indices in the given pairs any number of times. + * Return the lexicographically smallest string that s can be changed to after using the swaps. + */ + +class Solution { +public: + static std::string smallestStringWithSwaps(const std::string& s, const std::vector>& pairs) { + const int n = s.length(); + + // Disjoint Set + int* mother = new int[n]; + for (int i = 0; i < n; ++i) + mother[i] = i; + + std::function Find = [&](int x) { return mother[x] == x ? x : mother[x] = Find(mother[x]); }; + auto Union = [&](int x, int y) { mother[Find(x)] = Find(y); }; + + for (const auto& p : pairs) + Union(p[0], p[1]); + + std::unordered_map> m; + for (int i = 0; i < n; ++i) + m[Find(i)].push_back(i); + + std::string ans = s; + for (const auto& [i, v] : m) { + if (v.size() <= 1) + continue; + + int cnt[26]{}; + for (int idx : v) + ++cnt[s[idx] - 'a']; + + for (int ptr = 0, j = 0; ptr < 26; ++ptr) + while (cnt[ptr]--) + ans[v[j++]] = 'a' + ptr; + } + + return ans; + } +}; + +int main() { + auto ans = Solution::smallestStringWithSwaps("dcab", {{0,3},{1,2}}); + std::cout << ans << "\n"; + return 0; +}