/**
* Problem: LeetCode 1202 - Smallest String With Swaps
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.List;
class Solution {
private int[] parent;
private int find(int x) {
while (parent[x] != x) {
parent[x] = parent[parent[x]]; // path compression
x = parent[x];
}
return x;
}
public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
int n = s.length();
parent = new int[n];
for (int i = 0; i < n; i++) parent[i] = i;
// Jin indices ko swap kar sakte hain, unhe ek group mein jodo
for (List<Integer> p : pairs) {
int a = find(p.get(0));
int b = find(p.get(1));
if (a != b) parent[a] = b;
}
// Har group (root) ke liye uske characters ka frequency count
int[][] freq = new int[n][];
for (int i = 0; i < n; i++) {
int root = find(i);
if (freq[root] == null) freq[root] = new int[26];
freq[root][s.charAt(i) - 'a']++;
}
// Har index pe, uske group ka sabse chhota bacha hua character rakho
int[] ptr = new int[n]; // har root ke liye current smallest char pointer
char[] res = new char[n];
for (int i = 0; i < n; i++) {
int root = find(i);
int c = ptr[root];
while (freq[root][c] == 0) c++;
res[i] = (char) ('a' + c);
freq[root][c]--;
ptr[root] = c;
}
return new String(res);
}
}
"""
Problem: LeetCode 1202 - Smallest String With Swaps
Difficulty: Medium
Language: Python 3
Author: CodeByTushu
"""
class Solution:
def smallestStringWithSwaps(self, s: str, pairs: list[list[int]]) -> str:
n = len(s)
parent = list(range(n))
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for u, v in pairs:
ru, rv = find(u), find(v)
if ru != rv:
parent[ru] = rv
freq = {}
for i, ch in enumerate(s):
root = find(i)
if root not in freq:
freq[root] = [0] * 26
freq[root][ord(ch) - 97] += 1
ptr = {}
res = []
for i in range(n):
root = find(i)
c = ptr.get(root, 0)
counts = freq[root]
while counts[c] == 0:
c += 1
res.append(chr(97 + c))
counts[c] -= 1
ptr[root] = c
return "".join(res)
/**
* Problem: LeetCode 1202 - Smallest String With Swaps
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
vector<int> parent;
int find(int x) {
while (parent[x] != x) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
public:
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {
int n = static_cast<int>(s.size());
parent.resize(n);
for (int i = 0; i < n; i++) parent[i] = i;
for (const auto& p : pairs) {
int a = find(p[0]);
int b = find(p[1]);
if (a != b) parent[a] = b;
}
vector<vector<int>> freq(n);
for (int i = 0; i < n; i++) {
int root = find(i);
if (freq[root].empty()) freq[root].assign(26, 0);
freq[root][s[i] - 'a']++;
}
vector<int> ptr(n, 0);
string res(n, ' ');
for (int i = 0; i < n; i++) {
int root = find(i);
int c = ptr[root];
while (freq[root][c] == 0) c++;
res[i] = static_cast<char>('a' + c);
freq[root][c]--;
ptr[root] = c;
}
return res;
}
};
/**
* Problem: LeetCode 1202 - Smallest String With Swaps
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {string} s
* @param {number[][]} pairs
* @return {string}
*/
var smallestStringWithSwaps = function(s, pairs) {
const n = s.length;
const parent = new Int32Array(n);
for (let i = 0; i < n; i++) parent[i] = i;
const find = (x) => {
while (parent[x] !== x) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
};
for (let i = 0; i < pairs.length; i++) {
const a = find(pairs[i][0]);
const b = find(pairs[i][1]);
if (a !== b) parent[a] = b;
}
const freq = new Array(n);
for (let i = 0; i < n; i++) {
const root = find(i);
if (!freq[root]) freq[root] = new Int32Array(26);
freq[root][s.charCodeAt(i) - 97]++;
}
const ptr = new Int32Array(n);
const res = new Array(n);
for (let i = 0; i < n; i++) {
const root = find(i);
let c = ptr[root];
while (freq[root][c] === 0) c++;
res[i] = String.fromCharCode(97 + c);
freq[root][c]--;
ptr[root] = c;
}
return res.join("");
};