Skip to content

Commit 712344b

Browse files
author
Partho Biswas
committed
523_Continuous_Subarray_Sum.swift
+ 249_Group_Shifted_Strings.swift
1 parent 77be2f9 commit 712344b

File tree

4 files changed

+59
-31
lines changed

4 files changed

+59
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ I have solved quite a number of problems from several topics. See the below tabl
238238
|37| **[763. Partition Labels](https://tinyurl.com/yapt4n6v)** | [Python](https://tinyurl.com/wu6rdaw/763_Partition_Labels.py), [Swift](https://tinyurl.com/wuja3c4/763_Partition_Labels.swift) | | Medium | |
239239
|38| [819. Most Common Word](https://tinyurl.com/ydhwgc33) | [Python](https://tinyurl.com/wu6rdaw/819_Most_Common_Word.py), [Swift](https://tinyurl.com/wuja3c4/819_Most_Common_Word.swift) | [Art 1](https://tinyurl.com/y7u4pzeu) | Easy | |
240240
|39| [350. Intersection of Two Arrays II](https://tinyurl.com/y9kdrubz) | [Python](https://tinyurl.com/wu6rdaw/350_Intersection_of_Two_Arrays_II.py), [Swift](https://tinyurl.com/wuja3c4/350_Intersection_of_Two_Arrays_II.swift) | --- | Easy | Check the follow-ups |
241+
|40| **[249. Group Shifted Strings](https://tinyurl.com/jsfd4l3)** | [Python](https://tinyurl.com/wu6rdaw/249_Group_Shifted_Strings.py), [Swift](https://tinyurl.com/wuja3c4/249_Group_Shifted_Strings.swift) | [Art 1](https://tinyurl.com/yd4ckev2) | Medium | Tricky one |
241242

242243

243244
</p>
@@ -716,6 +717,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
716717
|52| **[304. Range Sum Query 2D - Immutable](https://tinyurl.com/ybhblo57)** | [Python](https://tinyurl.com/wu6rdaw/304_Range_Sum_Query_2D_Immutable.py), [Swift](https://tinyurl.com/wuja3c4/304_Range_Sum_Query_2D_Immutable.swift)| [Art 1](https://tinyurl.com/ycto2x5o), [Art 2](https://tinyurl.com/yab7byj5) | Medium | **very good question, classical** |
717718
|53| **[1477. Find Two Non-overlapping Sub-arrays Each With Target Sum](https://tinyurl.com/ybte7ydl)** | [Python](https://tinyurl.com/wu6rdaw/1477_Find_Two_Non_overlapping_Sub_arrays_Each_With_Target_Sum.py), [Swift](https://tinyurl.com/wuja3c4/1477_Find_Two_Non_overlapping_Sub_arrays_Each_With_Target_Sum.swift)| [Art 1](https://tinyurl.com/y7rshs2j), [Art 2](https://tinyurl.com/y8x7cgxl) | Medium | **Very interesting and deceiving and tricky problem. An unique combination of sliding window and DP** |
718719
|54| **[368. Largest Divisible Subset](https://tinyurl.com/ya8sv5jr)** | [Python](https://tinyurl.com/wu6rdaw/368_Largest_Divisible_Subset.py), [Swift](https://tinyurl.com/wuja3c4/368_Largest_Divisible_Subset.swift)| Official Solution | Medium | **Interesting problem.** |
720+
|55| **[523. Continuous Subarray Sum](https://tinyurl.com/y6ce49ln)** | [Python](https://tinyurl.com/wu6rdaw/523_Continuous_Subarray_Sum.py), [Swift](https://tinyurl.com/wuja3c4/523_Continuous_Subarray_Sum.swift)| []() | Medium | **Tricky to see the DP** |
719721

720722

721723
</p>
Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
1-
import Foundation
2-
class Solution {
3-
func exclusiveTime(_ n: Int, _ logs: [String]) -> [Int] {
4-
var funcTimeMap = Array(repeating: 0, count: n)
5-
var funcStack = [Int]()
6-
var prevTime = 0
7-
8-
for log in logs {
9-
let logItem = log.split(separator: ":")
10-
let (functionOptional, type, timeOptional) = (Int(logItem[0]), logItem[1], Int(logItem[2]))
11-
guard let function = functionOptional, let time = timeOptional else {continue}
12-
13-
// print("-----")
14-
// print("func: \(function), type: \(type), time: \(time), prevTime: \(prevTime)")
15-
// print(funcTimeMap)
16-
// print(funcStack)
17-
18-
if type == "start" {
19-
if funcStack = funcStack.count > 0 {
20-
funcTimeMap[funcStack.last!] += (time - prevTime)
21-
}
22-
prevTime = time
23-
funcStack.append(function)
24-
} else {
25-
funcTimeMap[funcStack.popLast()!] += (time - prevTime + 1)
26-
prevTime = time + 1
27-
}
28-
}
29-
return funcTimeMap
30-
}
31-
}
1+
2+
3+
4+
5+
6+
7+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Foundation
2+
3+
extension String {
4+
var patternKey: String {
5+
let asciiValue = self.unicodeScalars.map {Int($0.value)}
6+
let diffValue = asciiValue.map {(26 + $0 - asciiValue[0]) % 26}
7+
print("Diff Valets: \(diffValue)")
8+
return diffValue.reduce("", {$0 + " \($1)"})
9+
}
10+
}
11+
12+
class Solution {
13+
func groupStrings(_ strings: [String]) -> [[String]] {
14+
var groupMap = [Int:[String]]()
15+
strings.forEach { string in
16+
let patttern = string.patternKey
17+
print(patttern)
18+
groupMap[patttern] = (groupMap[patttern] ?? [String]()) + [string]
19+
}
20+
return Array(groupMap.values)
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
// Initial solution. Time complexity: O(n^2)
3+
import Foundation
4+
class Solution {
5+
func checkSubarraySum(_ nums: [Int], _ k: Int) -> Bool {
6+
guard nums.count > 1 else {return false}
7+
8+
for i in (0..<(nums.count) - 1) {
9+
var currentSum = nums[i]
10+
for j in ((i + 1)..<nums.count) {
11+
currentSum += nums[j]
12+
if k == 0 {
13+
if currentSum == 0 {
14+
print("1")
15+
return true
16+
} else {
17+
continue
18+
}
19+
} else if currentSum % abs(k) == 0 {
20+
print("3")
21+
return true
22+
}
23+
}
24+
}
25+
return false
26+
}
27+
}
28+
*/

0 commit comments

Comments
 (0)