Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
74 changes: 73 additions & 1 deletion articles/add-two-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,40 @@ class Solution {
}
```

```swift
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
private func add(_ l1: ListNode?, _ l2: ListNode?, _ carry: Int) -> ListNode? {
if l1 == nil && l2 == nil && carry == 0 {
return nil
}

let v1 = l1?.val ?? 0
let v2 = l2?.val ?? 0

let sum = v1 + v2 + carry
let newCarry = sum / 10
let val = sum % 10

let nextNode = add(l1?.next, l2?.next, newCarry)
return ListNode(val, nextNode)
}

func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
return add(l1, l2, 0)
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -577,11 +611,49 @@ class Solution {
}
```

```swift
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
let dummy = ListNode(0)
var cur = dummy
var l1 = l1, l2 = l2
var carry = 0

while l1 != nil || l2 != nil || carry != 0 {
let v1 = l1?.val ?? 0
let v2 = l2?.val ?? 0

let sum = v1 + v2 + carry
carry = sum / 10
let val = sum % 10
cur.next = ListNode(val)

cur = cur.next!
l1 = l1?.next
l2 = l2?.next
}
return dummy.next
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(m + n)$
* Space complexity: $O(1)$
* Space complexity:
* $O(1)$ extra space.
* $O(max(m, n))$ for the output list.

> Where $m$ is the length of $l1$ and $n$ is the length of $l2$.
39 changes: 37 additions & 2 deletions articles/anagram-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ class Solution {
}
```

```swift
class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var res = [String: [String]]()

for s in strs {
let sortedS = String(s.sorted())
res[sortedS, default: []].append(s)
}

return Array(res.values)
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -277,11 +292,31 @@ class Solution {
}
```

```swift
class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var res = [Array<Int>: [String]]()

for s in strs {
var count = [Int](repeating: 0, count: 26)
for c in s {
count[Int(c.asciiValue!) - 97] += 1
}
res[count, default: []].append(s)
}

return Array(res.values)
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(m * n)$
* Space complexity: $O(m)$
* Space complexity:
* $O(m)$ extra space.
* $O(m * n)$ space for the output list.

> Where $m$ is the number of strings and $n$ is the length of the longest string.
> Where $m$ is the number of strings and $n$ is the length of the longest string.
124 changes: 123 additions & 1 deletion articles/balanced-binary-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,43 @@ class Solution {
}
```

```swift
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func isBalanced(_ root: TreeNode?) -> Bool {
guard let root = root else { return true }

let left = height(root.left)
let right = height(root.right)

if abs(left - right) > 1 {
return false
}

return isBalanced(root.left) && isBalanced(root.right)
}

private func height(_ root: TreeNode?) -> Int {
guard let root = root else { return 0 }
return 1 + max(height(root.left), height(root.right))
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -521,6 +558,39 @@ class Solution {
}
```

```swift
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func isBalanced(_ root: TreeNode?) -> Bool {
return dfs(root).0
}

private func dfs(_ root: TreeNode?) -> (Bool, Int) {
guard let root = root else { return (true, 0) }

let left = dfs(root.left)
let right = dfs(root.right)

let balanced = left.0 && right.0 && abs(left.1 - right.1) <= 1
return (balanced, 1 + max(left.1, right.1))
}
}
```

::tabs-end

### Time & Space Complexity
Expand All @@ -534,7 +604,7 @@ class Solution {

---

## 3. Depth First Search (Stack)
## 3. Iterative DFS

::tabs-start

Expand Down Expand Up @@ -866,6 +936,58 @@ class Solution {
}
```

```swift
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func isBalanced(_ root: TreeNode?) -> Bool {
var stack = [TreeNode]()
var node = root
var last: TreeNode? = nil
var depths = [ObjectIdentifier: Int]()

while !stack.isEmpty || node != nil {
if let current = node {
stack.append(current)
node = current.left
} else {
guard let current = stack.last else { break }
if current.right == nil || last === current.right {
stack.removeLast()

let leftDepth = current.left != nil ? depths[ObjectIdentifier(current.left!)] ?? 0 : 0
let rightDepth = current.right != nil ? depths[ObjectIdentifier(current.right!)] ?? 0 : 0
if abs(leftDepth - rightDepth) > 1 {
return false
}

depths[ObjectIdentifier(current)] = 1 + max(leftDepth, rightDepth)
last = current
node = nil
} else {
node = current.right
}
}
}

return true
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
Loading