From 5aa5ba0e9da89f42a4e5bdab8d7e63778e626c6d Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Fri, 26 Apr 2024 21:44:11 +0900 Subject: [PATCH 1/6] feat: Add solution for LeetCode problem 217 Contains Duplicate --- best-time-to-buy-and-sell-stock/WhiteHyun.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/WhiteHyun.swift diff --git a/best-time-to-buy-and-sell-stock/WhiteHyun.swift b/best-time-to-buy-and-sell-stock/WhiteHyun.swift new file mode 100644 index 000000000..0017c5497 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/WhiteHyun.swift @@ -0,0 +1,15 @@ +// +// 217. Contains Duplicate.swift +// https://leetcode.com/problems/contains-duplicate/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode217 { + func containsDuplicate(_ nums: [Int]) -> Bool { + Set(nums).count != nums.count + } +} From e042fa37c58d98a4fc393f6e2ae35118c45c8fc0 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Fri, 26 Apr 2024 21:44:35 +0900 Subject: [PATCH 2/6] feat: Add LeetCode242 solution for Valid Anagram --- valid-anagram/WhiteHyun.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 valid-anagram/WhiteHyun.swift diff --git a/valid-anagram/WhiteHyun.swift b/valid-anagram/WhiteHyun.swift new file mode 100644 index 000000000..5e7903363 --- /dev/null +++ b/valid-anagram/WhiteHyun.swift @@ -0,0 +1,15 @@ +// +// 242. Valid Anagram.swift +// https://leetcode.com/problems/valid-anagram/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode242 { + func isAnagram(_ s: String, _ t: String) -> Bool { + Dictionary(s.map { ($0, 1) }, uniquingKeysWith: +) == Dictionary(t.map { ($0, 1) }, uniquingKeysWith: +) + } +} From 74353b8fd5863c1deb96941a3306f68ee92b31c0 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Fri, 26 Apr 2024 21:44:54 +0900 Subject: [PATCH 3/6] feat: add solution for Two Sum problem on LeetCode --- two-sum/WhiteHyun.swift | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 two-sum/WhiteHyun.swift diff --git a/two-sum/WhiteHyun.swift b/two-sum/WhiteHyun.swift new file mode 100644 index 000000000..6293a95d5 --- /dev/null +++ b/two-sum/WhiteHyun.swift @@ -0,0 +1,31 @@ +// +// 1. Two Sum.swift +// https://leetcode.com/problems/two-sum/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode1 { + func twoSum(_ numbers: [Int], _ target: Int) -> [Int] { + let sortedNumbersWithIndex = numbers.enumerated().sorted { lhs, rhs in + lhs.element < rhs.element + } + var left = 0 + var right = sortedNumbersWithIndex.endIndex - 1 + + while left < right { + let sum = sortedNumbersWithIndex[left].element + sortedNumbersWithIndex[right].element + if sum == target { break } + if sum < target { + left += 1 + } else { + right -= 1 + } + } + + return [sortedNumbersWithIndex[left].offset, sortedNumbersWithIndex[right].offset] + } +} From 31faf1335c33e29072f562575e0b880d13bff94f Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Fri, 26 Apr 2024 22:17:58 +0900 Subject: [PATCH 4/6] feat: Add solution for LeetCode problem 125 --- valid-palindrome/WhiteHyun.swift | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 valid-palindrome/WhiteHyun.swift diff --git a/valid-palindrome/WhiteHyun.swift b/valid-palindrome/WhiteHyun.swift new file mode 100644 index 000000000..38bd64ed1 --- /dev/null +++ b/valid-palindrome/WhiteHyun.swift @@ -0,0 +1,32 @@ +// +// 125. Valid Palindrome.swift +// https://leetcode.com/problems/valid-palindrome/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode125 { + func isPalindrome(_ s: String) -> Bool { + if let regex = try? NSRegularExpression(pattern: "[a-zA-Z0-9]+") { + let string = regex + .matches(in: s, range: .init(location: 0, length: s.count)) + .map { s[Range($0.range, in: s)!].lowercased() } + .joined() + + return string == String(string.reversed()) + } + + return false + } + + func isPalindromeUsingRegexString(_ s: String) -> Bool { + var alphabets = "" + for match in s.matches(of: /(?[a-zA-Z0-9]+)/) { + alphabets += match.alphabet.lowercased() + } + return String(alphabets.reversed()) == alphabets + } +} From 4de8394c79cace4acd565eb9be25c6db11a268f3 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Fri, 26 Apr 2024 22:30:55 +0900 Subject: [PATCH 5/6] feat: Add solution for LeetCode problem 121 --- contains-duplicate/WhiteHyun.swift | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 contains-duplicate/WhiteHyun.swift diff --git a/contains-duplicate/WhiteHyun.swift b/contains-duplicate/WhiteHyun.swift new file mode 100644 index 000000000..b463b8061 --- /dev/null +++ b/contains-duplicate/WhiteHyun.swift @@ -0,0 +1,28 @@ +// +// 121. Best Time to Buy and Sell Stock.swift +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode121 { + func maxProfit(_ prices: [Int]) -> Int { + if prices.count == 1 { return 0 } + var diff = 0 + var left = 0 + var right = 0 + + while right < prices.endIndex { + if prices[left] > prices[right] { + left = right + } else { + diff = max(diff, prices[right] - prices[left]) + } + right += 1 + } + return diff + } +} From dcd28513676c8e839493a7a3d7ffb61c3b6ba64b Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sat, 27 Apr 2024 17:18:49 +0900 Subject: [PATCH 6/6] move: Move file to correct directory --- .../WhiteHyun.swift | 23 +++++++++++++++---- contains-duplicate/WhiteHyun.swift | 23 ++++--------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/WhiteHyun.swift b/best-time-to-buy-and-sell-stock/WhiteHyun.swift index 0017c5497..b463b8061 100644 --- a/best-time-to-buy-and-sell-stock/WhiteHyun.swift +++ b/best-time-to-buy-and-sell-stock/WhiteHyun.swift @@ -1,6 +1,6 @@ // -// 217. Contains Duplicate.swift -// https://leetcode.com/problems/contains-duplicate/description/ +// 121. Best Time to Buy and Sell Stock.swift +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ // Algorithm // // Created by 홍승현 on 2024/04/26. @@ -8,8 +8,21 @@ import Foundation -final class LeetCode217 { - func containsDuplicate(_ nums: [Int]) -> Bool { - Set(nums).count != nums.count +final class LeetCode121 { + func maxProfit(_ prices: [Int]) -> Int { + if prices.count == 1 { return 0 } + var diff = 0 + var left = 0 + var right = 0 + + while right < prices.endIndex { + if prices[left] > prices[right] { + left = right + } else { + diff = max(diff, prices[right] - prices[left]) + } + right += 1 + } + return diff } } diff --git a/contains-duplicate/WhiteHyun.swift b/contains-duplicate/WhiteHyun.swift index b463b8061..0017c5497 100644 --- a/contains-duplicate/WhiteHyun.swift +++ b/contains-duplicate/WhiteHyun.swift @@ -1,6 +1,6 @@ // -// 121. Best Time to Buy and Sell Stock.swift -// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ +// 217. Contains Duplicate.swift +// https://leetcode.com/problems/contains-duplicate/description/ // Algorithm // // Created by 홍승현 on 2024/04/26. @@ -8,21 +8,8 @@ import Foundation -final class LeetCode121 { - func maxProfit(_ prices: [Int]) -> Int { - if prices.count == 1 { return 0 } - var diff = 0 - var left = 0 - var right = 0 - - while right < prices.endIndex { - if prices[left] > prices[right] { - left = right - } else { - diff = max(diff, prices[right] - prices[left]) - } - right += 1 - } - return diff +final class LeetCode217 { + func containsDuplicate(_ nums: [Int]) -> Bool { + Set(nums).count != nums.count } }