Skip to content

[doitduri] WEEK 03 solutions #1322

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 5 commits into from
Apr 22, 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
31 changes: 31 additions & 0 deletions combination-sum/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
if candidates.filter { $0 > target }.count == candidates.count {
return []
}

var result: [[Int]] = []
var current: [Int] = []

func backtrack(_ start: Int, _ remain: Int) {
if remain == 0 {
result.append(current)
return
}

if remain < 0 {
return
}

for i in start..<candidates.count {
current.append(candidates[i])
backtrack(i, remain - candidates[i])
current.removeLast()
}
}

backtrack(0, target)

return result
}
}
31 changes: 31 additions & 0 deletions decode-ways/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
func numDecodings(_ s: String) -> Int {
if s.first == "0" {
return 0
}

if s.count == 1 {
return 1
}

var tables = Array(repeating: 0, count: s.count+1)
let chars = Array(s)
tables[0] = 1
tables[1] = 1

for i in 2...s.count {
if chars[i-1] != "0" {
tables[i] += tables[i-1]
}

if let twoDigit = Int(String(chars[i-2...i-1])) {
if 10 <= twoDigit && twoDigit <= 26 {
tables[i] += tables[i-2]
}
}

}

return tables[s.count]
}
}
12 changes: 12 additions & 0 deletions maximum-subarray/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
func maxSubArray(_ nums: [Int]) -> Int {
var currentSum = nums[0]
var maxSum = nums[0]

for i in 1..<nums.count {
currentSum = max(nums[i], currentSum + nums[i])
maxSum = max(maxSum, currentSum)
}
return maxSum
}
}
6 changes: 6 additions & 0 deletions number-of-1-bits/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
func hammingWeight(_ n: Int) -> Int {
let bitString = String(n, radix: 2)
return bitString.filter { $0 == "1" }.count
}
}
6 changes: 6 additions & 0 deletions valid-palindrome/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
func isPalindrome(_ s: String) -> Bool {
let strings = s.filter { $0.isLetter || $0.isNumber }.lowercased()
return String(strings.reversed()) == strings
}
}