Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sri Hari: Batch-6/Neetcode-150/Added-SwiftCode#3936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
neetcode-gh merged 13 commits intoneetcode-gh:mainfromSrihari2222:develop_swift
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
9ad559a
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 11, 2025
70123dc
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 11, 2025
d887b0c
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 12, 2025
d3d3398
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 12, 2025
14af280
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 12, 2025
ffe1e47
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 13, 2025
41d19f7
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 13, 2025
9e8d758
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 14, 2025
ae943bc
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 14, 2025
6898cff
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 15, 2025
aee8159
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 15, 2025
0a4e0d1
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 16, 2025
030abdd
Batch-6/Neetcode-150/Added-swiftcode
Srihari2222Mar 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
Batch-6/Neetcode-150/Added-swiftcode
  • Loading branch information
@Srihari2222
Srihari2222 committedMar 16, 2025
commit030abddf87dc9a55cb409373a10c01571dbceefe
124 changes: 123 additions & 1 deletionarticles/count-connected-components.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -229,6 +229,41 @@ class Solution {
}
```

```swift
class Solution {
func countComponents(_ n: Int, _ edges: [[Int]]) -> Int {
var adj = Array(repeating: [Int](), count: n)
var visit = Array(repeating: false, count: n)

for edge in edges {
let u = edge[0], v = edge[1]
adj[u].append(v)
adj[v].append(u)
}

func dfs(_ node: Int) {
for nei in adj[node] {
if !visit[nei] {
visit[nei] = true
dfs(nei)
}
}
}

var res = 0
for node in 0..<n {
if !visit[node] {
visit[node] = true
dfs(node)
res += 1
}
}

return res
}
}
```

::tabs-end

### Time & Space Complexity
Expand DownExpand Up@@ -506,6 +541,44 @@ class Solution {
}
```

```swift
class Solution {
func countComponents(_ n: Int, _ edges: [[Int]]) -> Int {
var adj = Array(repeating: [Int](), count: n)
var visit = Array(repeating: false, count: n)
for edge in edges {
let u = edge[0], v = edge[1]
adj[u].append(v)
adj[v].append(u)
}

func bfs(_ node: Int) {
var q = Deque<Int>()
q.append(node)
visit[node] = true
while !q.isEmpty {
let cur = q.removeFirst()
for nei in adj[cur] {
if !visit[nei] {
visit[nei] = true
q.append(nei)
}
}
}
}

var res = 0
for node in 0..<n {
if !visit[node] {
bfs(node)
res += 1
}
}
return res
}
}
```

::tabs-end

### Time & Space Complexity
Expand DownExpand Up@@ -556,7 +629,7 @@ class Solution:
```

```java
publicclass DSU {
class DSU {
int[] parent;
int[] rank;

Expand DownExpand Up@@ -877,6 +950,55 @@ class Solution {
}
```

```swift
class DSU {
var parent: [Int]
var rank: [Int]

init(_ n: Int) {
parent = Array(0..<n)
rank = Array(repeating: 1, count: n)
}

func find(_ node: Int) -> Int {
var cur = node
while cur != parent[cur] {
parent[cur] = parent[parent[cur]]
cur = parent[cur]
}
return cur
}

func union(_ u: Int, _ v: Int) -> Bool {
let pu = find(u)
let pv = find(v)
if pu == pv { return false }
var rootU = pu
var rootV = pv
if rank[rootV] > rank[rootU] {
swap(&rootU, &rootV)
}
parent[rootV] = rootU
rank[rootU] += rank[rootV]
return true
}
}

class Solution {
func countComponents(_ n: Int, _ edges: [[Int]]) -> Int {
let dsu = DSU(n)
var res = n
for edge in edges {
let u = edge[0], v = edge[1]
if dsu.union(u, v) {
res -= 1
}
}
return res
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
122 changes: 122 additions & 0 deletionsarticles/foreign-dictionary.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -405,6 +405,65 @@ class Solution {
}
```

```swift
class Solution {
func foreignDictionary(_ words: [String]) -> String {
var adj = [Character: Set<Character>]()
for word in words {
for char in word {
if adj[char] == nil {
adj[char] = Set<Character>()
}
}
}

for i in 0..<words.count - 1 {
let w1 = words[i]
let w2 = words[i + 1]
let minLen = min(w1.count, w2.count)
if w1.count > w2.count && String(w1.prefix(minLen)) == String(w2.prefix(minLen)) {
return ""
}
for j in 0..<minLen {
let index1 = w1.index(w1.startIndex, offsetBy: j)
let index2 = w2.index(w2.startIndex, offsetBy: j)
if w1[index1] != w2[index2] {
adj[w1[index1]]?.insert(w2[index2])
break
}
}
}

var visited = [Character: Bool]()
var res = [Character]()

func dfs(_ char: Character) -> Bool {
if let flag = visited[char] {
return flag
}
visited[char] = true
for neigh in adj[char]! {
if dfs(neigh) {
return true
}
}
visited[char] = false
res.append(char)
return false
}

for char in adj.keys {
if dfs(char) {
return ""
}
}

res.reverse()
return String(res)
}
}
```

::tabs-end

### Time & Space Complexity
Expand DownExpand Up@@ -823,6 +882,69 @@ class Solution {
}
```

```swift
class Solution {
func foreignDictionary(_ words: [String]) -> String {
var adj = [Character: Set<Character>]()
for word in words {
for char in word {
adj[char] = Set<Character>()
}
}

var indegree = [Character: Int]()
for key in adj.keys {
indegree[key] = 0
}

for i in 0..<words.count - 1 {
let w1 = words[i]
let w2 = words[i + 1]
let minLen = min(w1.count, w2.count)
if w1.count > w2.count && String(w1.prefix(minLen)) == String(w2.prefix(minLen)) {
return ""
}
let w1Arr = Array(w1)
let w2Arr = Array(w2)
for j in 0..<minLen {
if w1Arr[j] != w2Arr[j] {
if !adj[w1Arr[j]]!.contains(w2Arr[j]) {
adj[w1Arr[j]]!.insert(w2Arr[j])
indegree[w2Arr[j]]! += 1
}
break
}
}
}

var q = Deque<Character>()
for (c, deg) in indegree {
if deg == 0 {
q.append(c)
}
}

var res = [Character]()
while !q.isEmpty {
let char = q.removeFirst()
res.append(char)
for neighbor in adj[char]! {
indegree[neighbor]! -= 1
if indegree[neighbor]! == 0 {
q.append(neighbor)
}
}
}

if res.count != indegree.count {
return ""
}

return String(res)
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp