diff --git a/best-time-to-buy-and-sell-stock/delight010.swift b/best-time-to-buy-and-sell-stock/delight010.swift new file mode 100644 index 0000000000..8e00a14164 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/delight010.swift @@ -0,0 +1,12 @@ +class Solution { + func maxProfit(_ prices: [Int]) -> Int { + var lowPrice: Int = prices[0] + var maxProfit: Int = 0 + for i in 0.. [[String]] { + var dict: [[Character: Int]: [String]] = [:] + for str in strs { + var strDict: [Character: Int] = [:] + for char in str { + if let charCount = strDict[char] { + strDict[char] = charCount + 1 + } else { + strDict[char] = 1 + } + } + if dict[strDict] != nil { + dict[strDict]?.append(str) + } else { + dict[strDict] = [str] + } + } + + return dict.values.map { $0 } + } +} + diff --git a/implement-trie-prefix-tree/delight010.swift b/implement-trie-prefix-tree/delight010.swift new file mode 100644 index 0000000000..4c4e3fe0cb --- /dev/null +++ b/implement-trie-prefix-tree/delight010.swift @@ -0,0 +1,47 @@ +class Trie { + var nodes: [String: Trie] = [:] + var isEndOfWord: Bool = false + + init() { + + } + + func insert(_ word: String) { + if word.isEmpty { + isEndOfWord = true + return + } + + let firstChar = String(word.first!) + if nodes[firstChar] == nil { + nodes[firstChar] = Trie() + } + nodes[firstChar]?.insert(String(word.dropFirst())) + } + + func search(_ word: String) -> Bool { + if word.isEmpty { + return isEndOfWord + } + + guard let firstChar = word.first, + let node = nodes[String(firstChar)] else { + return false + } + + return node.search(String(word.dropFirst())) + } + + func startsWith(_ prefix: String) -> Bool { + if prefix.isEmpty { + return true + } + + guard let firstChar = prefix.first, + let node = nodes[String(firstChar)] else { + return false + } + return node.startsWith(String(prefix.dropFirst())) + } +} + diff --git a/word-break/delight010.swift b/word-break/delight010.swift new file mode 100644 index 0000000000..04edea7c33 --- /dev/null +++ b/word-break/delight010.swift @@ -0,0 +1,20 @@ +class Solution { + func wordBreak(_ s: String, _ wordDict: [String]) -> Bool { + var wordArray = Array(repeating: false, count: s.count + 1) + wordArray[0] = true + for i in 1...s.count { + for j in 0..