Skip to content

Commit 788de79

Browse files
committed
leetcode problem solved related to stack, interval, pointers & hasmaps.
1 parent 0429a48 commit 788de79

File tree

41 files changed

+597
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+597
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/two-sum/
4+
5+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
6+
var hashMap = [Int : Int]()
7+
8+
for (index, value) in nums.enumerated() {
9+
if hashMap[value] != nil {
10+
return [hashMap[value]!, index]
11+
} else {
12+
hashMap[target - value] = index
13+
}
14+
}
15+
return []
16+
}
17+
18+
print(twoSum([2,7,11,15], 9))
19+
print(twoSum([3,2,4], 6))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/longest-consecutive-sequence/
4+
5+
func longestConsecutive(_ nums: [Int]) -> Int {
6+
var numSet = Set(nums)
7+
var longest = 0
8+
9+
for num in nums {
10+
if !numSet.contains(num - 1) {
11+
var length = 0
12+
while numSet.contains(num + length) {
13+
length += 1
14+
longest = max(longest, length)
15+
}
16+
}
17+
}
18+
19+
return longest
20+
}
21+
22+
print(longestConsecutive([100,4,200,1,3,2]))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

DataStrucutureQuestions/HashMap/166. Fraction to Recurring Decimal.playground/Contents.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import UIKit
22

33
//https://leetcode.com/problems/fraction-to-recurring-decimal/
44

5-
6-
75
func fractionToDecimal(_ numerator: Int, _ denominator: Int) -> String {
86
var result = ""
97
var remainderMap : [Int : Int] = [:]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/happy-number/
4+
5+
func isHappy(_ n: Int) -> Bool {
6+
var slow = n
7+
var fast = n
8+
9+
while true {
10+
slow = getNext(slow)
11+
fast = getNext(fast)
12+
fast = getNext(fast)
13+
if slow == fast {
14+
break
15+
}
16+
}
17+
return fast == 1
18+
}
19+
20+
private func getNext(_ n: Int) -> Int {
21+
var sum = 0
22+
var input = n
23+
24+
while input > 0 {
25+
let remainder = input % 10
26+
sum += remainder * remainder
27+
input /= 10
28+
}
29+
30+
return sum
31+
}
32+
print(isHappy(19))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

DataStrucutureQuestions/HashMap/205. Isomorphic Strings.playground/Contents.swift

Lines changed: 22 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/contains-duplicate-ii/
4+
5+
func containsNearbyDuplicate(_ nums: [Int], _ k: Int) -> Bool {
6+
var dict = [Int : Int]()
7+
for (index, value) in nums.enumerated() {
8+
if let duplicateIndex = dict[value], index - duplicateIndex <= k {
9+
return true
10+
}
11+
dict[value] = index
12+
}
13+
return false
14+
}
15+
16+
print(containsNearbyDuplicate([1,2,3,1], 3))
17+
print(containsNearbyDuplicate([1,2,3,1,2,3], 2))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/valid-anagram/
4+
5+
func isAnagram(_ s: String, _ t: String) -> Bool {
6+
var sCountHashMap = [Character: Int]()
7+
var tCountHashMap = [Character: Int]()
8+
9+
if s.count != t.count {
10+
return false
11+
}
12+
13+
for index in s.indices {
14+
sCountHashMap[s[index]] = 1 + sCountHashMap[s[index], default: 0]
15+
tCountHashMap[t[index]] = 1 + tCountHashMap[t[index], default: 0]
16+
}
17+
18+
for key in sCountHashMap.keys {
19+
if sCountHashMap[key] != tCountHashMap[key] {
20+
return false
21+
}
22+
}
23+
return true
24+
}
25+
print(isAnagram("anagram", "nagaram"))
26+
print(isAnagram("rat", "car"))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/word-pattern/
4+
5+
func wordPattern(_ pattern: String, _ s: String) -> Bool {
6+
var hashMap = [Character : String.SubSequence]()
7+
var patternArray = Array(pattern)
8+
var sArray = s.split(separator: " ")
9+
if patternArray.count != sArray.count {
10+
return false
11+
}
12+
for i in 0..<sArray.count {
13+
if hashMap[patternArray[i]] == nil {
14+
hashMap[patternArray[i]] = sArray[i]
15+
} else if hashMap[patternArray[i]] != sArray[i] {
16+
return false
17+
}
18+
}
19+
return hashMap.keys.count == Set(hashMap.values).count
20+
}
21+
22+
print(wordPattern("abba", "dog cat cat dog"))
23+
print(wordPattern("abba", "dog cat cat fish"))
24+
print(wordPattern("aaaa", "dog cat cat dog"))
25+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/ransom-note/description/
4+
5+
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
6+
var hashMap = [Character : Int]()
7+
for character in magazine {
8+
if hashMap[character] == nil {
9+
hashMap[character] = 1
10+
} else {
11+
hashMap[character]! += 1
12+
}
13+
}
14+
15+
for character in ransomNote {
16+
if let count = hashMap[character], count > 0 {
17+
hashMap[character]! -= 1
18+
} else {
19+
return false
20+
}
21+
}
22+
return true
23+
}
24+
print(canConstruct("a", "b"))
25+
print(canConstruct("aa", "ab"))
26+
print(canConstruct("aa", "aab"))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/longest-palindrome/description/
4+
5+
func longestPalindrome(_ s: String) -> Int {
6+
var hashMap = [Character : Int]()
7+
var palindromCount = 0
8+
for character in s {
9+
if hashMap[character] == nil {
10+
hashMap[character] = 1
11+
} else {
12+
hashMap[character]! += 1
13+
}
14+
}
15+
for (_, element) in hashMap {
16+
if element % 2 == 0 {
17+
palindromCount += element
18+
} else {
19+
palindromCount += element - 1
20+
}
21+
}
22+
return palindromCount < s.count ? palindromCount + 1 : palindromCount
23+
}
24+
25+
print(longestPalindrome("abccccdddeeeeeeff"))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/group-anagrams/
4+
5+
func groupAnagrams(_ strs: [String]) -> [[String]] {
6+
var result = [String: [String]]()
7+
8+
for string in strs {
9+
var countArray = [Int](repeating: 0, count: 26)
10+
11+
for character in string {
12+
countArray[Int(character.asciiValue! - Character("a").asciiValue!)] += 1
13+
}
14+
let key = countArray.map { String($0) }.joined(separator: ",")
15+
result[key, default: []].append(string)
16+
}
17+
return Array(result.values)
18+
}
19+
20+
print(groupAnagrams(["eat","tea","tan","ate","nat","bat"]))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/next-greater-element-i/description/
4+
5+
func nextGreaterElement(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
6+
var stacks : [Int] = []
7+
var mapping = [Int : Int]()
8+
9+
for (_, value) in nums2.enumerated() {
10+
while !stacks.isEmpty && value > stacks.last ?? 0 {
11+
mapping[stacks.removeLast()] = value
12+
}
13+
stacks.append(value)
14+
}
15+
16+
while !stacks.isEmpty {
17+
mapping[stacks.removeLast()] = -1
18+
}
19+
20+
var answer = [Int]()
21+
22+
for num in nums1 {
23+
answer.append(mapping[num]!)
24+
}
25+
return answer
26+
}
27+
28+
print(nextGreaterElement([4,1,2], [1,3,4,2]))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/summary-ranges/
4+
5+
func summaryRanges(_ nums: [Int]) -> [String] {
6+
var start = 0
7+
var end = 0
8+
var outPutAray = [String]()
9+
if nums.count == 1 {
10+
return ["\(nums[start])"]
11+
}
12+
while end < nums.count - 1 {
13+
if nums[end + 1] - nums[end] == 1 {
14+
end += 1
15+
} else {
16+
outPutAray.append(start != end ? "\(nums[start])->\(nums[end])" : "\(nums[end])")
17+
end += 1
18+
start = end
19+
}
20+
if nums[end] == nums.last {
21+
outPutAray.append(start != end ? "\(nums[start])->\(nums[end])" : "\(nums[end])")
22+
}
23+
}
24+
return outPutAray
25+
}
26+
27+
print(summaryRanges([0,2,3,4,6,8,9]))
28+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
4+
5+
func findMinArrowShots(_ points: [[Int]]) -> Int {
6+
var pointsSorted = points.sorted(by: { $0[1] < $1[1] })
7+
var end = pointsSorted[0][1]
8+
var arrows = 1
9+
10+
11+
for point in pointsSorted {
12+
if point[0] > end {
13+
arrows += 1
14+
end = point[1]
15+
}
16+
}
17+
18+
return arrows
19+
}
20+
21+
22+
print(findMinArrowShots([[10,16],[2,8],[1,6],[7,12]]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)