Skip to content

Commit a3dd46b

Browse files
committed
Solved some of array & string problem from top 150 interview Questions.
1 parent 0e9d2b4 commit a3dd46b

File tree

8 files changed

+134
-0
lines changed

8 files changed

+134
-0
lines changed
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/gas-station/?envType=study-plan-v2&envId=top-interview-150
4+
5+
func canCompleteCircuit(_ gas: [Int], _ cost: [Int]) -> Int {
6+
var result = 0
7+
var total = 0
8+
if gas.reduce(0, +) < cost.reduce(0, +) {
9+
return -1
10+
}
11+
for index in 0..<gas.count {
12+
total += (gas[index] - cost[index])
13+
if total < 0 {
14+
total = 0
15+
result = index + 1
16+
}
17+
}
18+
return result
19+
}
20+
21+
print("starting gas station's index is :- \(canCompleteCircuit([1,2,3,4,5], [3,4,5,1,2]))")
22+
print("starting gas station's index is :- \(canCompleteCircuit([2,3,4], [3,4,3]))")
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/text-justification/description/?envType=study-plan-v2&envId=top-interview-150
4+
5+
func fullJustify(_ words: [String], _ maxWidth: Int) -> [String] {
6+
var result : [String] = []
7+
var line : [String] = [], length = 0
8+
var index = 0
9+
10+
while index < words.count {
11+
if length + line.count + words[index].count > maxWidth {
12+
//Line Complete
13+
14+
var extraSpace = maxWidth - length
15+
16+
let spaces = extraSpace / max(1, line.count - 1)
17+
var remainder = extraSpace % max(1, line.count - 1)
18+
19+
for j in 0..<max(1, line.count - 1) {
20+
line[j] += String(repeating: " ", count: spaces)
21+
if remainder > 0 {
22+
line[j] += " "
23+
remainder -= 1
24+
}
25+
}
26+
result.append(line.joined(separator: ""))
27+
line = []
28+
length = 0 //reset line & length
29+
}
30+
line.append(words[index])
31+
length += words[index].count
32+
index += 1
33+
}
34+
//Handling last line
35+
var last_line = line.joined(separator: " ")
36+
var trail_Space = maxWidth - last_line.count
37+
38+
result.append(last_line + String(repeating: " ", count: trail_Space))
39+
40+
return result
41+
}
42+
43+
print(fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16))
44+
print(fullJustify(["What","must","be","acknowledgment","shall","be"], 16))
45+
print(fullJustify(["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], 20))
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,20 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/?envType=study-plan-v2&envId=top-interview-150
4+
5+
func strStr(_ haystack: String, _ needle: String) -> Int {
6+
let lengthOfHay = haystack.count, lengthOfN = needle.count
7+
if lengthOfHay == 0 || haystack == needle { return 0 }
8+
guard lengthOfHay >= lengthOfN else { return -1 }
9+
var indexInitialOfHay = haystack.startIndex
10+
11+
for pointer in 0...(lengthOfHay - lengthOfN) {
12+
var endIndex = haystack.index(indexInitialOfHay, offsetBy : lengthOfN)
13+
if haystack[indexInitialOfHay..<endIndex] == needle { return pointer }
14+
indexInitialOfHay = haystack.index(after: indexInitialOfHay)
15+
}
16+
return -1
17+
}
18+
19+
print("Index of First occurance of sad in String sadbutsad is :- \(strStr("sadbutsad", "sad"))")
20+
print("Index of First occurance of leeto in String leetcode is :- \(strStr("leetcode", "leeto"))")
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/zigzag-conversion/description/?envType=study-plan-v2&envId=top-interview-150
4+
5+
func convert(_ s: String, _ numRows: Int) -> String {
6+
if numRows == 1 {
7+
return s
8+
}
9+
var res = ""
10+
let increment = 2 * (numRows - 1)
11+
//print("increament = \(increment)")
12+
for r in 0..<numRows {
13+
var i = r
14+
//print("in for loop i = \(i)")
15+
//print("in for loop r = \(r)")
16+
while i < s.count {
17+
//print("in while loop i = \(i)")
18+
res += String(s[s.index(s.startIndex, offsetBy: i)])
19+
//print("in while loop res = \(res)")
20+
if r > 0 && r < numRows - 1 && i + increment - 2 * r < s.count {
21+
res += String(s[s.index(s.startIndex, offsetBy: i + increment - 2 * r)])
22+
//print("in if condition res = \(res)")
23+
}
24+
i += increment
25+
}
26+
}
27+
return res
28+
}
29+
30+
print("ZigZag conversion of PAYPALISHIRING is :- \(convert("PAYPALISHIRING", 3))")
31+
print("ZigZag conversion of PAYPALISHIRING is :- \(convert("PAYPALISHIRING", 4))")
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>

0 commit comments

Comments
 (0)