-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallestStringWithSwaps.py
More file actions
40 lines (33 loc) · 966 Bytes
/
smallestStringWithSwaps.py
File metadata and controls
40 lines (33 loc) · 966 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/smallest-string-with-swaps/
# Author: Miao Zhang
# Date: 2021-04-17
class Solution:
def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:
ss = list(s)
n = len(s)
g = [[] for _ in range(n)]
for u, v in pairs:
g[u].append(v)
g[v].append(u)
seen = set()
idx = []
t = []
def dfs(i):
if i in seen: return
seen.add(i)
idx.append(i)
t.append(s[i])
for nxt in g[i]:
dfs(nxt)
for i in range(n):
if i in seen: continue
idx.clear()
t.clear()
dfs(i)
idx.sort()
t.sort()
for k in range(len(idx)):
ss[idx[k]] = t[k]
return ''.join(ss)