From c27e3b99c8f0ac3264c6a0d1ac3ae6329413ddd7 Mon Sep 17 00:00:00 2001 From: Jinyoung Jeong <80400463+jyoung2419@users.noreply.github.com> Date: Sat, 24 Oct 2026 19:31:23 +0900 Subject: [PATCH] Create 1202. Smallest String With Swaps --- .../1202. Smallest String With Swaps" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "leetcode3/\354\240\225\354\247\204\354\230\201/1202. Smallest String With Swaps" diff --git "a/leetcode3/\354\240\225\354\247\204\354\230\201/1202. Smallest String With Swaps" "b/leetcode3/\354\240\225\354\247\204\354\230\201/1202. Smallest String With Swaps" new file mode 100644 index 00000000..b3c5a1b3 --- /dev/null +++ "b/leetcode3/\354\240\225\354\247\204\354\230\201/1202. Smallest String With Swaps" @@ -0,0 +1,31 @@ +/** + * @param {string} s + * @param {number[][]} pairs + * @return {string} + */ +var smallestStringWithSwaps = function(s, pairs) { + /* + 1. 아이디어 + - pairs를 이용해 인덱스 간 연결 관계를 만든다 + - dfs 로 서로 연결된 인덱스를 찾는다 + - 연결된 인덱스에 해당하는 문자들을 오름차순 정렬 후 반환 + 2. 시간복잡도 + + 3. 자료구조 + - chk : 이미 방문한 인덱스인지 확인용 + - + */ + + let graph = Array.from({ length: s.length }, () => []); + let chk = Array.from(s.length).fill(0); + + + for (let i = 0; i < pairs.length; i++) { + let a = pairs[i][0]; + let b = pairs[i][1]; + + graph[a].push(b); + graph[b].push(a); + } + +};