Skip to content

Commit 3b1d72f

Browse files
authored
Merge pull request #1846 from delight010/main
[SeongA] WEEK 5 solutions
2 parents 3610c37 + 8a2b330 commit 3b1d72f

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func maxProfit(_ prices: [Int]) -> Int {
3+
var lowPrice: Int = prices[0]
4+
var maxProfit: Int = 0
5+
for i in 0..<prices.endIndex {
6+
lowPrice = min(lowPrice, prices[i])
7+
maxProfit = max(maxProfit, prices[i] - lowPrice)
8+
}
9+
return maxProfit
10+
}
11+
}
12+

group-anagrams/delight010.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func groupAnagrams(_ strs: [String]) -> [[String]] {
3+
var dict: [[Character: Int]: [String]] = [:]
4+
for str in strs {
5+
var strDict: [Character: Int] = [:]
6+
for char in str {
7+
if let charCount = strDict[char] {
8+
strDict[char] = charCount + 1
9+
} else {
10+
strDict[char] = 1
11+
}
12+
}
13+
if dict[strDict] != nil {
14+
dict[strDict]?.append(str)
15+
} else {
16+
dict[strDict] = [str]
17+
}
18+
}
19+
20+
return dict.values.map { $0 }
21+
}
22+
}
23+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Trie {
2+
var nodes: [String: Trie] = [:]
3+
var isEndOfWord: Bool = false
4+
5+
init() {
6+
7+
}
8+
9+
func insert(_ word: String) {
10+
if word.isEmpty {
11+
isEndOfWord = true
12+
return
13+
}
14+
15+
let firstChar = String(word.first!)
16+
if nodes[firstChar] == nil {
17+
nodes[firstChar] = Trie()
18+
}
19+
nodes[firstChar]?.insert(String(word.dropFirst()))
20+
}
21+
22+
func search(_ word: String) -> Bool {
23+
if word.isEmpty {
24+
return isEndOfWord
25+
}
26+
27+
guard let firstChar = word.first,
28+
let node = nodes[String(firstChar)] else {
29+
return false
30+
}
31+
32+
return node.search(String(word.dropFirst()))
33+
}
34+
35+
func startsWith(_ prefix: String) -> Bool {
36+
if prefix.isEmpty {
37+
return true
38+
}
39+
40+
guard let firstChar = prefix.first,
41+
let node = nodes[String(firstChar)] else {
42+
return false
43+
}
44+
return node.startsWith(String(prefix.dropFirst()))
45+
}
46+
}
47+

word-break/delight010.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {
3+
var wordArray = Array(repeating: false, count: s.count + 1)
4+
wordArray[0] = true
5+
for i in 1...s.count {
6+
for j in 0..<i {
7+
if wordArray[j] {
8+
let startIndex = s.index(s.startIndex, offsetBy: j)
9+
let endIndex = s.index(s.startIndex, offsetBy: i)
10+
let word = String(s[startIndex..<endIndex])
11+
if wordDict.contains(word) {
12+
wordArray[i] = true
13+
}
14+
}
15+
}
16+
}
17+
return wordArray[s.count]
18+
}
19+
}
20+

0 commit comments

Comments
 (0)