Skip to content

Commit 843963c

Browse files
author
Partho Biswas
committed
no message
1 parent 87de331 commit 843963c

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ I have solved quite a number of problems from several topics. See the below tabl
235235
|36| **[273. Integer to English Words](https://tinyurl.com/y7pps4u9)** | [Python](https://tinyurl.com/wu6rdaw/273_Integer_to_English_Words.py), [Swift](https://tinyurl.com/wuja3c4/273_Integer_to_English_Words.swift) | | Hard | MUST check again |
236236
|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 | |
237237
|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 | |
238+
|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 |
238239

239240

240241
</p>
Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
# from collections import defaultdict
2+
# class Solution(object):
3+
# def lengthOfLongestSubstringKDistinct(self, s, k):
4+
# """
5+
# :type s: str
6+
# :type k: int
7+
# :rtype: int
8+
# """
9+
# if not s or len(s) <= 0 or not k or k == 0:
10+
# return 0
11+
# charCountDict = defaultdict(int)
12+
# startIdx = 0
13+
# longestSubStrLen = 0
14+
# for endIdx, currentChar in enumerate(s):
15+
# if len(charCountDict) < k:
16+
# charCountDict[currentChar] += 1
17+
# elif len(charCountDict) == k:
18+
# if currentChar in charCountDict:
19+
# charCountDict[currentChar] += 1
20+
# else:
21+
# while len(charCountDict) == k:
22+
# charToRemove = s[startIdx]
23+
# charCountDict[charToRemove] -= 1
24+
# if charCountDict[charToRemove] == 0:
25+
# del charCountDict[charToRemove]
26+
# startIdx += 1
27+
# charCountDict[currentChar] = 1
28+
# longestSubStrLen = max(longestSubStrLen, endIdx - startIdx + 1)
29+
# return longestSubStrLen
30+
31+
32+
# My solution during Mock
133
from collections import defaultdict
234

335

@@ -8,25 +40,27 @@ def lengthOfLongestSubstringKDistinct(self, s, k):
840
:type k: int
941
:rtype: int
1042
"""
11-
if not s or len(s) <= 0 or not k or k == 0:
43+
if len(s) <= 0 or k == 0:
1244
return 0
13-
charCountDict = defaultdict(int)
14-
startIdx = 0
15-
longestSubStrLen = 0
16-
for endIdx, currentChar in enumerate(s):
17-
if len(charCountDict) < k:
18-
charCountDict[currentChar] += 1
19-
elif len(charCountDict) == k:
20-
if currentChar in charCountDict:
21-
charCountDict[currentChar] += 1
22-
else:
23-
while len(charCountDict) == k:
24-
charToRemove = s[startIdx]
25-
charCountDict[charToRemove] -= 1
26-
if charCountDict[charToRemove] == 0:
27-
del charCountDict[charToRemove]
28-
startIdx += 1
29-
charCountDict[currentChar] = 1
30-
longestSubStrLen = max(longestSubStrLen, endIdx - startIdx + 1)
31-
return longestSubStrLen
45+
left, right, maxLen = 0, 0, float("-inf")
46+
frequencies = defaultdict(int)
47+
while right < len(s):
48+
frequencies[s[right]] += 1
49+
while len(frequencies) > k:
50+
frequencies[s[left]] -= 1
51+
if frequencies[s[left]] == 0:
52+
del frequencies[s[left]]
53+
left += 1
54+
maxLen = max(maxLen, right - left + 1)
55+
right += 1
56+
return maxLen
57+
58+
59+
"""
60+
length of the longest substring contains at most k distinct characters >> longest substring
61+
62+
"eceba"
3263
64+
e:1
65+
b:1
66+
"""
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from collections import Counter
2+
3+
4+
class Solution(object):
5+
def intersect(self, nums1, nums2):
6+
"""
7+
:type nums1: List[int]
8+
:type nums2: List[int]
9+
:rtype: List[int]
10+
"""
11+
count1 = Counter(nums1)
12+
count2 = Counter(nums2)
13+
result = []
14+
15+
for key in count1:
16+
if key in count2:
17+
minFreq = min(count1[key], count2[key])
18+
for i in range(minFreq):
19+
result.append(key)
20+
21+
return result

0 commit comments

Comments
 (0)