Skip to content

[SeongA] WEEK 5 solutions #1846

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 4 commits into from
Aug 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
12 changes: 12 additions & 0 deletions best-time-to-buy-and-sell-stock/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
func maxProfit(_ prices: [Int]) -> Int {
var lowPrice: Int = prices[0]
var maxProfit: Int = 0
for i in 0..<prices.endIndex {
lowPrice = min(lowPrice, prices[i])
maxProfit = max(maxProfit, prices[i] - lowPrice)
}
return maxProfit
}
}

23 changes: 23 additions & 0 deletions group-anagrams/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
func groupAnagrams(_ strs: [String]) -> [[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 }
}
}

47 changes: 47 additions & 0 deletions implement-trie-prefix-tree/delight010.swift
Original file line number Diff line number Diff line change
@@ -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()))
}
}

20 changes: 20 additions & 0 deletions word-break/delight010.swift
Original file line number Diff line number Diff line change
@@ -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..<i {
if wordArray[j] {
let startIndex = s.index(s.startIndex, offsetBy: j)
let endIndex = s.index(s.startIndex, offsetBy: i)
let word = String(s[startIndex..<endIndex])
if wordDict.contains(word) {
wordArray[i] = true
}
}
}
}
return wordArray[s.count]
}
}