More codes
swadhin biswas committed 1 years ago
ed4e897
1 parent 2b21c8b
commit ed4e897
10 files changed
+416 -0
Showing 10 changed files+416-0
187. Repeated DNA Sequences Medium.py187.Repeted DNA Squence.py209. Minimum Size Subarray Sum Medium.py220. Contains Duplicate III.py30. Substring with Concatenation of All Words Hard.py76. Minimum Window Substring.pyContains Duplicate II.pyLongest Substring Without Repeating Characters Solved.pytest.ipynbtower of hanoi algorithm.py
▼187. Repeated DNA Sequences Medium.py
+59-0
| @@ -0,0 +1,59 @@ | ||
1 | +class Solution: | |
2 | + def findRepeatedDnaSequences(self, s: str) -> List[str]: | |
3 | + lengthofs=len(s) | |
4 | + j=10 | |
5 | + hashset=set() | |
6 | + repetedsequence=set() | |
7 | + for i in range(lengthofs): | |
8 | + if s[i:j] not in hashset: | |
9 | + hashset.add(s[i:j]) | |
10 | + else: | |
11 | + repetedsequence.add(s[i:j]) | |
12 | + if j==lengthofs: | |
13 | + break | |
14 | + j+=1 | |
15 | + return list(repetedsequence) | |
16 | + | |
17 | + | |
18 | +class Solution: | |
19 | + def findRepeatedDnaSequences(self, s: str) -> List[str]: | |
20 | + if len(s) < 10 or len(s) >= 10**5: | |
21 | + return [] | |
22 | + | |
23 | + WINDOWSIZE = 10 | |
24 | + WINDOWSIZE0 = 9 | |
25 | + BASE = 31 | |
26 | + BASE10 = pow(31, WINDOWSIZE0) | |
27 | + MOD = (10**9) + 7 | |
28 | + T = lambda x: ord(x) - 40 | |
29 | + | |
30 | + h = 0 | |
31 | + | |
32 | + # encode | |
33 | + for i, char in enumerate(s[:WINDOWSIZE]): | |
34 | + h += (T(char) * pow(BASE, WINDOWSIZE0-i)) | |
35 | + | |
36 | + h = h % MOD | |
37 | + seen = set({h}) | |
38 | + res = set() | |
39 | + | |
40 | + for i in range(1, len(s) - (WINDOWSIZE0)): | |
41 | + # remove first char/bit | |
42 | + h -= (T(s[i-1]) * BASE10) % MOD | |
43 | + | |
44 | + # 1 power to each char -> (c1*BASE^9 + ... c10*BASE^0) * BASE -> (c1*BASE^10 + ... c10*BASE^1) | |
45 | + # (c1*BASE^10 + ... c10*BASE^1) + c11*BASE^0 | |
46 | + char = s[i+WINDOWSIZE0] | |
47 | + h = (h * BASE + T(char)) % MOD | |
48 | + | |
49 | + # print(f"removing {s[i-1]} [{T(s[i-1])}] | adding {char} [{T(char)}] | s={s[i:i+WINDOWSIZE]} | {h=}, {(T(s[i-1]) * BASE10)}") | |
50 | + | |
51 | + if h in seen: | |
52 | + res.add(s[i:i+WINDOWSIZE]) | |
53 | + | |
54 | + seen.add(h) | |
55 | + | |
56 | + # print(seen) | |
57 | + return list(res) | |
58 | + | |
59 | + | |
0 | 60 | |
▼187.Repeted DNA Squence.py
+0-0
0 | 0 |
▼209. Minimum Size Subarray Sum Medium.py
+10-0
| @@ -0,0 +1,10 @@ | ||
1 | +class Solution: | |
2 | + def minSubArrayLen(self, target: int, nums: List[int]) -> int: | |
3 | + right,sumof,index=0,0,float('inf') | |
4 | + for left in range(len(nums)): | |
5 | + sumof+=nums[left] | |
6 | + while sumof>=target: | |
7 | + index=min(index,left-right+1) | |
8 | + s-=nums[right] | |
9 | + right+=1 | |
10 | + return 0 if index==float('inf') else index | |
0 | 11 | |
▼220. Contains Duplicate III.py
+23-0
| @@ -0,0 +1,23 @@ | ||
1 | +class Solution: | |
2 | + def containsNearbyAlmostDuplicate(self, nums: List[int], indexDiff: int, valueDiff: int) -> bool: | |
3 | + if indexDiff<0 or valueDiff<0: | |
4 | + return False | |
5 | + d={} | |
6 | + bucket_lenth=indexDiff+1 | |
7 | + for i, num in enumerate(nums): | |
8 | + bucket_index=num//bucket_lenth | |
9 | + | |
10 | + if bucket_index in d : | |
11 | + return True | |
12 | + if bucket_index-1 in d and abs(num-d[bucket_index-1])<=valueDiff: | |
13 | + return True | |
14 | + if bucket_index+1 in d and abs(num-d[bucket_index+1])<=valueDiff: | |
15 | + return True | |
16 | + | |
17 | + d[bucket_index]=num | |
18 | + if i>=indexDiff: | |
19 | + oldindex=nums[i-indexDiff]//bucket_lenth | |
20 | + if oldindex in d and d[oldindex]==nums[i-indexDiff]: | |
21 | + del d[oldindex] | |
22 | + | |
23 | + return False | |
0 | 24 | |
▼30. Substring with Concatenation of All Words Hard.py
+37-0
| @@ -0,0 +1,37 @@ | ||
1 | +class Solution: | |
2 | + def findSubstring(self, s: str, words: List[str]) -> List[int]: | |
3 | + if not s or not words: | |
4 | + return [] | |
5 | + | |
6 | + n = len(s) | |
7 | + f_count = Counter(words) | |
8 | + word_l = len(words[0]) | |
9 | + target = len(words) | |
10 | + ans = [] | |
11 | + | |
12 | + for i in range(word_l): | |
13 | + left, right = i, i | |
14 | + tem = deque([]) | |
15 | + cur_count = Counter() | |
16 | + | |
17 | + while right + word_l <= n: | |
18 | + clip = s[right: right + word_l] | |
19 | + right += word_l | |
20 | + | |
21 | + if clip in f_count: | |
22 | + tem.append(clip) | |
23 | + cur_count[clip] += 1 | |
24 | + | |
25 | + while cur_count[clip] > f_count[clip]: | |
26 | + cur_count[tem.popleft()] -= 1 | |
27 | + left += word_l | |
28 | + | |
29 | + if len(tem) == target: | |
30 | + ans.append(left) | |
31 | + else: | |
32 | + tem.clear() | |
33 | + cur_count.clear() | |
34 | + left = right | |
35 | + | |
36 | + return ans | |
37 | + | |
0 | 38 | |
▼76. Minimum Window Substring.py
+5-0
| @@ -0,0 +1,5 @@ | ||
1 | +class Solution: | |
2 | + def minWindow(self, s: str, t: str) -> str: | |
3 | + pass | |
4 | + | |
5 | + | |
0 | 6 | |
▼Contains Duplicate II.py
+57-0
| @@ -0,0 +1,57 @@ | ||
1 | +class Solution: | |
2 | + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: | |
3 | + numslen=len(nums) | |
4 | + d=set() | |
5 | + j=0 | |
6 | + for i in range(numslen): | |
7 | + if i-j>k: | |
8 | + d.remove(nums[j]) | |
9 | + j+=1 | |
10 | + if nums[i] in d: | |
11 | + return True | |
12 | + d.add(nums[i]) | |
13 | + return False | |
14 | + | |
15 | + | |
16 | +""" | |
17 | +Dry Run | |
18 | +when i=0 j=0 | |
19 | +and if 0-0<k | |
20 | + | |
21 | + | |
22 | + | |
23 | + | |
24 | + | |
25 | + | |
26 | + | |
27 | +""" | |
28 | + | |
29 | +class Solution: | |
30 | + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: | |
31 | + if len(set(nums))==len(nums): | |
32 | + return False | |
33 | + if len(nums)<=k+1: | |
34 | + return True | |
35 | + d={} | |
36 | + for i in range(len(nums)): | |
37 | + if nums[i] in d: | |
38 | + d[nums[i]]=abs(d[nums[i]]-i) | |
39 | + if d[nums[i]]<=k: | |
40 | + return True | |
41 | + else: | |
42 | + d[nums[i]]=i | |
43 | + else: | |
44 | + d[nums[i]]=i | |
45 | + return False | |
46 | + | |
47 | + | |
48 | + | |
49 | +class Solution: | |
50 | + def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: | |
51 | + if len(set(nums))==len(nums): | |
52 | + return False | |
53 | + if len(nums)<=k+1: | |
54 | + return True | |
55 | + | |
56 | + for i in range(len(nums)): | |
57 | + pass | |
0 | 58 | |
▼Longest Substring Without Repeating Characters Solved.py
+16-0
| @@ -0,0 +1,16 @@ | ||
1 | +class Solution: | |
2 | + def lengthOfLongestSubstring(self, s: str) -> int: | |
3 | + hashmap={} # take a hashmap | |
4 | + maxlength=0 # set the max length to 0 | |
5 | + left=0 # set variable left to 0 | |
6 | + for right in range(0,len(s)): # Run loop to range (0.len s) | |
7 | + while s[right] in hashmap: # cheack the right exist in Hasmap or not | |
8 | + hashmap.remove(s[left]) # if exist remove the left one | |
9 | + left+=1 # increse the left +1 | |
10 | + | |
11 | + hashmap.add(s[right]) # add the right one | |
12 | + maxlength=max(maxlength,right-left+1) # find the max length between current | |
13 | + #max length and the current position of your right pointer | |
14 | + | |
15 | + | |
16 | + return maxlength # return the max length | |
0 | 17 | |
▼test.ipynb
+170-0
| @@ -4043379,6 +4043379,176 @@ | ||
4043379 | 4043379 | "\n", |
4043380 | 4043380 | "print(threeSum(nums))" |
4043381 | 4043381 | ] |
4043382 | + }, | |
4043383 | + { | |
4043384 | + "cell_type": "code", | |
4043385 | + "execution_count": 2, | |
4043386 | + "metadata": {}, | |
4043387 | + "outputs": [ | |
4043388 | + { | |
4043389 | + "name": "stdout", | |
4043390 | + "output_type": "stream", | |
4043391 | + "text": [ | |
4043392 | + "AAAAACCCCC\n" | |
4043393 | + ] | |
4043394 | + } | |
4043395 | + ], | |
4043396 | + "source": [ | |
4043397 | + "s = \"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT\"\n", | |
4043398 | + "\n", | |
4043399 | + "i=0\n", | |
4043400 | + "j=10\n", | |
4043401 | + "\n", | |
4043402 | + "print(s[i:j])" | |
4043403 | + ] | |
4043404 | + }, | |
4043405 | + { | |
4043406 | + "cell_type": "code", | |
4043407 | + "execution_count": 13, | |
4043408 | + "metadata": {}, | |
4043409 | + "outputs": [ | |
4043410 | + { | |
4043411 | + "name": "stdout", | |
4043412 | + "output_type": "stream", | |
4043413 | + "text": [ | |
4043414 | + "set()\n", | |
4043415 | + "['CCCCCAAAAA', 'AAAAACCCCC']\n" | |
4043416 | + ] | |
4043417 | + } | |
4043418 | + ], | |
4043419 | + "source": [ | |
4043420 | + "from typing import List\n", | |
4043421 | + "\n", | |
4043422 | + "def findRepeatedDnaSequences(s: str) -> List[str]:\n", | |
4043423 | + " lengthofs=len(s)\n", | |
4043424 | + " j=10\n", | |
4043425 | + " hashset=set()\n", | |
4043426 | + " print(hashset)\n", | |
4043427 | + " repetedsequence=set()\n", | |
4043428 | + " for i in range(lengthofs):\n", | |
4043429 | + " if s[i:j] in hashset:\n", | |
4043430 | + " repetedsequence.add(s[i:j])\n", | |
4043431 | + "\n", | |
4043432 | + " else:\n", | |
4043433 | + " hashset.add(s[i:j])\n", | |
4043434 | + "\n", | |
4043435 | + " if j==lengthofs:\n", | |
4043436 | + " break\n", | |
4043437 | + " j+=1\n", | |
4043438 | + " return list(repetedsequence)\n", | |
4043439 | + "\n", | |
4043440 | + "\n", | |
4043441 | + "\n", | |
4043442 | + "s = \"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT\"\n", | |
4043443 | + "\n", | |
4043444 | + "print(findRepeatedDnaSequences(s))\n" | |
4043445 | + ] | |
4043446 | + }, | |
4043447 | + { | |
4043448 | + "cell_type": "code", | |
4043449 | + "execution_count": 10, | |
4043450 | + "metadata": {}, | |
4043451 | + "outputs": [ | |
4043452 | + { | |
4043453 | + "name": "stdout", | |
4043454 | + "output_type": "stream", | |
4043455 | + "text": [ | |
4043456 | + "False\n" | |
4043457 | + ] | |
4043458 | + } | |
4043459 | + ], | |
4043460 | + "source": [ | |
4043461 | + "from typing import List\n", | |
4043462 | + "\n", | |
4043463 | + "def containsNearbyDuplicate(nums: List[int], k: int) -> bool:\n", | |
4043464 | + " numslen=len(nums)\n", | |
4043465 | + " d=set()\n", | |
4043466 | + " j=0\n", | |
4043467 | + " for i in range(numslen):\n", | |
4043468 | + " if abs(i-j)>k:\n", | |
4043469 | + " d.remove(nums[i])\n", | |
4043470 | + " j+=1\n", | |
4043471 | + " if nums[i] in d:\n", | |
4043472 | + " return True\n", | |
4043473 | + " d.add(nums[i])\n", | |
4043474 | + " return False\n", | |
4043475 | + "\n", | |
4043476 | + "\n", | |
4043477 | + "\n", | |
4043478 | + "nums = [1,2,3,1,2,3]; k = 2\n", | |
4043479 | + "\n", | |
4043480 | + "print(containsNearbyDuplicate(nums,k))\n" | |
4043481 | + ] | |
4043482 | + }, | |
4043483 | + { | |
4043484 | + "cell_type": "code", | |
4043485 | + "execution_count": null, | |
4043486 | + "metadata": {}, | |
4043487 | + "outputs": [ | |
4043488 | + { | |
4043489 | + "ename": "IndexError", | |
4043490 | + "evalue": "list index out of range", | |
4043491 | + "output_type": "error", | |
4043492 | + "traceback": [ | |
4043493 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
4043494 | + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", | |
4043495 | + "Cell \u001b[0;32mIn[11], line 25\u001b[0m\n\u001b[1;32m 22\u001b[0m indexDiff \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m\n\u001b[1;32m 23\u001b[0m valueDiff \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m\n\u001b[0;32m---> 25\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcontainsNearbyAlmostDuplicate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnums\u001b[49m\u001b[43m,\u001b[49m\u001b[43mindexDiff\u001b[49m\u001b[43m,\u001b[49m\u001b[43mvalueDiff\u001b[49m\u001b[43m)\u001b[49m)\n", | |
4043496 | + "Cell \u001b[0;32mIn[11], line 11\u001b[0m, in \u001b[0;36mSolution.containsNearbyAlmostDuplicate\u001b[0;34m(self, nums, indexDiff, valueDiff)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(numslen):\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m i\u001b[38;5;241m+\u001b[39mindexDiff\u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39mnumslen:\n\u001b[0;32m---> 11\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mabs\u001b[39m(nums[i]\u001b[38;5;241m-\u001b[39m\u001b[43mnums\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43mindexDiff\u001b[49m\u001b[43m]\u001b[49m)\u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39mvalueDiff:\n\u001b[1;32m 12\u001b[0m d\u001b[38;5;241m.\u001b[39madd(nums[i\u001b[38;5;241m+\u001b[39mindexDiff])\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m d:\n", | |
4043497 | + "\u001b[0;31mIndexError\u001b[0m: list index out of range" | |
4043498 | + ] | |
4043499 | + } | |
4043500 | + ], | |
4043501 | + "source": [ | |
4043502 | + "from typing import List\n", | |
4043503 | + "\n", | |
4043504 | + "\n", | |
4043505 | + "class Solution:\n", | |
4043506 | + " def containsNearbyAlmostDuplicate(self, nums: List[int], indexDiff: int, valueDiff: int) -> bool:\n", | |
4043507 | + " numslen=len(nums)\n", | |
4043508 | + " d=set()\n", | |
4043509 | + " for i in range(numslen):\n", | |
4043510 | + " j=i+indexDiff\n", | |
4043511 | + " if j < numslen:\n", | |
4043512 | + " if abs(nums[i]-nums[j])\n", | |
4043513 | + "\n", | |
4043514 | + "\n", | |
4043515 | + " return False\n", | |
4043516 | + "\n", | |
4043517 | + "\n", | |
4043518 | + "x=Solution()\n", | |
4043519 | + "nums = [1,5,9,1,5,9]\n", | |
4043520 | + "indexDiff = 2\n", | |
4043521 | + "valueDiff = 3\n", | |
4043522 | + "\n", | |
4043523 | + "print(x.containsNearbyAlmostDuplicate(nums,indexDiff,valueDiff))" | |
4043524 | + ] | |
4043525 | + }, | |
4043526 | + { | |
4043527 | + "cell_type": "code", | |
4043528 | + "execution_count": 21, | |
4043529 | + "metadata": {}, | |
4043530 | + "outputs": [ | |
4043531 | + { | |
4043532 | + "name": "stdout", | |
4043533 | + "output_type": "stream", | |
4043534 | + "text": [ | |
4043535 | + "[1, 3, 7, 12, 18, 25, 33, 42]\n" | |
4043536 | + ] | |
4043537 | + } | |
4043538 | + ], | |
4043539 | + "source": [ | |
4043540 | + "# prefix sum\n", | |
4043541 | + "\n", | |
4043542 | + "text=[1,2,4,5,6,7,8,9]\n", | |
4043543 | + "\n", | |
4043544 | + "for i in range(1,len(text)):\n", | |
4043545 | + " text[i]=text[i-1]+text[i]\n", | |
4043546 | + "\n", | |
4043547 | + "\n", | |
4043548 | + "\n", | |
4043549 | + "\n", | |
4043550 | + "print(text)\n" | |
4043551 | + ] | |
4043382 | 4043552 | } |
4043383 | 4043553 | ], |
4043384 | 4043554 | "metadata": { |
4043385 | 4043555 | |
▼tower of hanoi algorithm.py
+39-0
| @@ -0,0 +1,39 @@ | ||
1 | +def tower_of_hanoi_array_simulation(n: int, source: list, auxiliary: list, target: list, move_count: list) -> None: | |
2 | + if n == 0: | |
3 | + return | |
4 | + tower_of_hanoi_array_simulation(n - 1, source, target, auxiliary, move_count) | |
5 | + if source: | |
6 | + disk_to_move = source.pop() | |
7 | + target.append(disk_to_move) | |
8 | + move_count[0] += 1 | |
9 | + print(f"Move {move_count[0]}: Move disk {disk_to_move} from Source to Target") | |
10 | + print(f" Current state: Source={source}, Auxiliary={auxiliary}, Target={target}\n") | |
11 | + else: | |
12 | + print(f"Error: Source array is unexpectedly empty when trying to move disk {n}.") | |
13 | + return | |
14 | + tower_of_hanoi_array_simulation(n - 1, auxiliary, source, target, move_count) | |
15 | + | |
16 | +if __name__ == "__main__": | |
17 | + initial_array_A = [1, 2, 3, 4, 5] | |
18 | + print(f"Original Array A: {initial_array_A}") | |
19 | + source_peg_array = initial_array_A[::-1] # [5, 4, 3, 2, 1] | |
20 | + auxiliary_peg_array = [] | |
21 | + target_peg_array = [] | |
22 | + | |
23 | + num_elements_to_move = len(initial_array_A) | |
24 | + total_moves = [0] | |
25 | + print("\n--- Starting Tower of Hanoi Array Simulation ---") | |
26 | + print(f"Initial Peg States:") | |
27 | + print(f" Source Peg (A): {source_peg_array}") | |
28 | + print(f" Auxiliary Peg (B): {auxiliary_peg_array}") | |
29 | + print(f" Target Peg (C): {target_peg_array}\n") | |
30 | + | |
31 | + tower_of_hanoi_array_simulation(num_elements_to_move, source_peg_array, auxiliary_peg_array, target_peg_array, total_moves) | |
32 | + | |
33 | + print("\n--- Simulation Complete! ---") | |
34 | + print("\nFinal Peg States:") | |
35 | + print(f" Source Peg (A): {source_peg_array}") | |
36 | + print(f" Auxiliary Peg (B): {auxiliary_peg_array}") | |
37 | + print(f" Target Peg (C): {target_peg_array}") | |
38 | + print(f"Total moves: {total_moves[0]}") | |
39 | + print(f"Target Array (reversed to match original order if 1 is smallest): {target_peg_array[::-1]}") | |
0 | 40 | |