diff --git a/combination-sum/doitduri.swift b/combination-sum/doitduri.swift new file mode 100644 index 000000000..56861acfb --- /dev/null +++ b/combination-sum/doitduri.swift @@ -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.. 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] + } +} diff --git a/maximum-subarray/doitduri.swift b/maximum-subarray/doitduri.swift new file mode 100644 index 000000000..34de6df0b --- /dev/null +++ b/maximum-subarray/doitduri.swift @@ -0,0 +1,12 @@ +class Solution { + func maxSubArray(_ nums: [Int]) -> Int { + var currentSum = nums[0] + var maxSum = nums[0] + + for i in 1.. Int { + let bitString = String(n, radix: 2) + return bitString.filter { $0 == "1" }.count + } +} diff --git a/valid-palindrome/doitduri.swift b/valid-palindrome/doitduri.swift new file mode 100644 index 000000000..b9654768f --- /dev/null +++ b/valid-palindrome/doitduri.swift @@ -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 + } +}