From f8ebc59b56edd408f3e5f29d55f5271d84ddb498 Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:14:38 +0700 Subject: [PATCH 01/11] Solved for issue #98 --- 394/README.md | 4 ++++ 394/leetcode394-DecodeString.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 394/README.md create mode 100644 394/leetcode394-DecodeString.py diff --git a/394/README.md b/394/README.md new file mode 100644 index 0000000..6fc2c5f --- /dev/null +++ b/394/README.md @@ -0,0 +1,4 @@ +# Hi, SpasZahariev! Here is my help . My code is on Python3 scenario +# Use Regex==regular expression is a sequence of characters that specifies a search pattern. + + diff --git a/394/leetcode394-DecodeString.py b/394/leetcode394-DecodeString.py new file mode 100644 index 0000000..ee1223e --- /dev/null +++ b/394/leetcode394-DecodeString.py @@ -0,0 +1,12 @@ +class Solution: + def decodeString(self, s: str) -> str: + import re + patt = "(\d+)\[([a-z]+)\]" + + found = re.findall(patt, s) + while found: + for rep, cnt in found: + s = s.replace(f'{rep}[{cnt}]', cnt * int(rep)) + found = re.findall(patt, s) + + return s \ No newline at end of file From e15a0bfe14d942dc6602e8361a965a956fcb3a39 Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:18:33 +0700 Subject: [PATCH 02/11] Solved for issue #94 --- 1232/README.md | 3 +++ 1232/leetcode1232.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 1232/README.md create mode 100644 1232/leetcode1232.py diff --git a/1232/README.md b/1232/README.md new file mode 100644 index 0000000..db45f47 --- /dev/null +++ b/1232/README.md @@ -0,0 +1,3 @@ +# Hi, rahulvansh66! Here is my help . My code is on Python3 scenario + + diff --git a/1232/leetcode1232.py b/1232/leetcode1232.py new file mode 100644 index 0000000..26b1a2c --- /dev/null +++ b/1232/leetcode1232.py @@ -0,0 +1,14 @@ +import math +class Solution: + def checkStraightLine(self, coord: List[List[int]]) -> bool: + coord = sorted(coord) + x = [] + for i in range(len(coord)-1): + if((coord[i][0] - coord[i+1][0])==0): + if((coord[i][1] - coord[i+1][1])>0): + x.append(math.atan(float("inf"))) + elif((coord[i][1] - coord[i+1][1])<0): + x.append(math.atan(float("-inf"))) + else: + x.append(math.atan((coord[i][1] - coord[i+1][1])/(coord[i][0] - coord[i+1][0]))) + return True if(len(set(x))== 1) else False \ No newline at end of file From f13fd5d17a21fbd3478f57a39f19439d2c326c1e Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 17:26:31 +0700 Subject: [PATCH 03/11] 392 decode string --- 394/leet394.py | 27 +++++++++++++++++++++++++++ 394/readme.md | 1 + 2 files changed, 28 insertions(+) create mode 100644 394/leet394.py create mode 100644 394/readme.md diff --git a/394/leet394.py b/394/leet394.py new file mode 100644 index 0000000..b69982e --- /dev/null +++ b/394/leet394.py @@ -0,0 +1,27 @@ +class Solution:class Solution: + def decodeString(self, s: str) -> str: + # Time: O(n) + # Space: O(n) + + stack = [] + + for c in s: + if c != ']': + stack.append(c) + else: + print(stack) + string = "" + num = "" + temp = "" + + while stack[-1] != '[': + string = stack.pop() + string + stack.pop() + + while stack and stack[-1].isdigit(): + num = stack.pop() + num + + for i in range(int(num)): + temp += string + stack.append(temp) + return "".join(stack) \ No newline at end of file diff --git a/394/readme.md b/394/readme.md new file mode 100644 index 0000000..4462edb --- /dev/null +++ b/394/readme.md @@ -0,0 +1 @@ +Hi, SpasZahariev! Here is my help . My code is on Python3 scenario . \ No newline at end of file From 0596781b1e4e32b560c2c3e9b8da2be2809c7edc Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 19:33:03 +0700 Subject: [PATCH 04/11] Delete leetcode394-DecodeString.py --- 394/leetcode394-DecodeString.py | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 394/leetcode394-DecodeString.py diff --git a/394/leetcode394-DecodeString.py b/394/leetcode394-DecodeString.py deleted file mode 100644 index ee1223e..0000000 --- a/394/leetcode394-DecodeString.py +++ /dev/null @@ -1,12 +0,0 @@ -class Solution: - def decodeString(self, s: str) -> str: - import re - patt = "(\d+)\[([a-z]+)\]" - - found = re.findall(patt, s) - while found: - for rep, cnt in found: - s = s.replace(f'{rep}[{cnt}]', cnt * int(rep)) - found = re.findall(patt, s) - - return s \ No newline at end of file From 6f60d99a4ff582edf11e5b29e7f5cf3df1ba27b8 Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 19:33:58 +0700 Subject: [PATCH 05/11] Delete README.md --- 394/README.md | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 394/README.md diff --git a/394/README.md b/394/README.md deleted file mode 100644 index 6fc2c5f..0000000 --- a/394/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Hi, SpasZahariev! Here is my help . My code is on Python3 scenario -# Use Regex==regular expression is a sequence of characters that specifies a search pattern. - - From 0a9bbe35cf6d5068d07d53e344bc98ed74a46487 Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 19:38:22 +0700 Subject: [PATCH 06/11] Update leetcode1232.py --- 1232/leetcode1232.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/1232/leetcode1232.py b/1232/leetcode1232.py index 26b1a2c..69f6746 100644 --- a/1232/leetcode1232.py +++ b/1232/leetcode1232.py @@ -1,14 +1,15 @@ -import math class Solution: - def checkStraightLine(self, coord: List[List[int]]) -> bool: - coord = sorted(coord) - x = [] - for i in range(len(coord)-1): - if((coord[i][0] - coord[i+1][0])==0): - if((coord[i][1] - coord[i+1][1])>0): - x.append(math.atan(float("inf"))) - elif((coord[i][1] - coord[i+1][1])<0): - x.append(math.atan(float("-inf"))) - else: - x.append(math.atan((coord[i][1] - coord[i+1][1])/(coord[i][0] - coord[i+1][0]))) - return True if(len(set(x))== 1) else False \ No newline at end of file + def checkStraightLine(self, A: List[List[int]]) -> bool: + results = [] + for i in A: + for j in A: + try: + results.append( + (i[1]-j[1]) / (i[0]-j[0]) + ) + except: + pass + return all([ + i == results[0] + for i in results + ]) From 0aeda758372bd59057371f2ece46a824b4c2f4c6 Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:18:51 +0700 Subject: [PATCH 07/11] Update leet394.py --- 394/leet394.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/394/leet394.py b/394/leet394.py index b69982e..5ffb957 100644 --- a/394/leet394.py +++ b/394/leet394.py @@ -24,4 +24,5 @@ def decodeString(self, s: str) -> str: for i in range(int(num)): temp += string stack.append(temp) - return "".join(stack) \ No newline at end of file + return "".join(stack) + From 87c6c37e8ee0ea6abbd3ef8a070c71f7ce7b9262 Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:20:13 +0700 Subject: [PATCH 08/11] Update leet394.py --- 394/leet394.py | 1 - 1 file changed, 1 deletion(-) diff --git a/394/leet394.py b/394/leet394.py index 5ffb957..1ad1f48 100644 --- a/394/leet394.py +++ b/394/leet394.py @@ -25,4 +25,3 @@ def decodeString(self, s: str) -> str: temp += string stack.append(temp) return "".join(stack) - From 29d47f0a559a5600b760cbe2919e72c3ebec8adc Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:56:25 +0700 Subject: [PATCH 09/11] Update leet394.py --- 394/leet394.py | 55 ++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/394/leet394.py b/394/leet394.py index 1ad1f48..a3c101b 100644 --- a/394/leet394.py +++ b/394/leet394.py @@ -1,27 +1,34 @@ -class Solution:class Solution: +class Solution: def decodeString(self, s: str) -> str: - # Time: O(n) - # Space: O(n) + ## assuming a valid string is provided s != ("1[a", "2[[2ac]]") + def get_char(): + res = "" + while stack and not stack[-1] == '[': + res = stack.pop() + res + return res - stack = [] - - for c in s: - if c != ']': - stack.append(c) - else: - print(stack) - string = "" - num = "" - temp = "" - - while stack[-1] != '[': - string = stack.pop() + string - stack.pop() + def get_num(): + num = "" + while stack and stack[-1].isdigit(): + num = stack.pop() + num + return num - while stack and stack[-1].isdigit(): - num = stack.pop() + num - - for i in range(int(num)): - temp += string - stack.append(temp) - return "".join(stack) + final= "" + stack = [] + for ele in s: + if ele == "]": # pop routine + res = get_char() + stack.pop() # removing the "[" bracket + num = get_num() + if num: + res = int(num) * res + if stack == []: + final += res + else: + stack.append(res) + else: + stack.append(ele) + if stack: + final += ''.join(stack) + return final + From 0c5c411450537ac8597479de3e1e12b0ca69b2ee Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:56:29 +0700 Subject: [PATCH 10/11] Update leetcode1232.py --- 1232/leetcode1232.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/1232/leetcode1232.py b/1232/leetcode1232.py index 69f6746..87c2ab0 100644 --- a/1232/leetcode1232.py +++ b/1232/leetcode1232.py @@ -1,15 +1,6 @@ +from typing import List class Solution: - def checkStraightLine(self, A: List[List[int]]) -> bool: - results = [] - for i in A: - for j in A: - try: - results.append( - (i[1]-j[1]) / (i[0]-j[0]) - ) - except: - pass - return all([ - i == results[0] - for i in results - ]) + def checkStraightLine(self, coords: List[List[int]]) -> bool: + if len(set(x for x, y in coords)) == 1: return True + if len(set(x for x, y in coords)) < len(coords): return False + return len(set((p1[1] - p2[1])/(p1[0] - p2[0]) for p1, p2 in zip(coords, coords[1:]))) == 1 From 8b7426cf2cdcafefc16ae4a0a883b18fc759e039 Mon Sep 17 00:00:00 2001 From: nghia pham <74634299+nghiab1706989@users.noreply.github.com> Date: Wed, 27 Oct 2021 20:57:29 +0700 Subject: [PATCH 11/11] update leet394.py update 394 --- 394/leet394.py | 1 - 1 file changed, 1 deletion(-) diff --git a/394/leet394.py b/394/leet394.py index a3c101b..66d87ab 100644 --- a/394/leet394.py +++ b/394/leet394.py @@ -31,4 +31,3 @@ def get_num(): if stack: final += ''.join(stack) return final -