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); + } + +};