From 9aa394457567cd2b42a5bc3e5850e854580d60e4 Mon Sep 17 00:00:00 2001 From: glowfi <86939591+glowfi@users.noreply.github.com> Date: Wed, 7 Jun 2023 23:38:22 +0530 Subject: [PATCH 1/2] Create 1071-greatest-common-divisor-of-strings --- .../1071-greatest-common-divisor-of-strings | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 python/1071-greatest-common-divisor-of-strings diff --git a/python/1071-greatest-common-divisor-of-strings b/python/1071-greatest-common-divisor-of-strings new file mode 100644 index 000000000..d2f482f78 --- /dev/null +++ b/python/1071-greatest-common-divisor-of-strings @@ -0,0 +1,42 @@ +class Solution: + def checkDivisible(self,str1, str2, x): + i, j = 0, 0 + f, f1 = 0, 0 + + # Optimizations + if len(str1)%len(x)!=0 or len(str2)%len(x)!=0: + return False + + + # Build String 1 by concatenating x + tmp = "" + while i <= len(str1): + tmp += x + if tmp == str1: + f = 1 + i += len(x) + + # Build String 2 by concatenating x + tmp = "" + while j <= len(str2): + tmp += x + if tmp == str2: + f1 = 1 + j += len(x) + + if f == 1 and f1 == 1: + return True + return False + + def gcdOfStrings(self, str1: str, str2: str) -> str: + ans="" + + # Always making sure that str1 remains the string with lesser concatenating + if str1 > str2: + str1, str2 = str2, str1 + + for i in range(len(str1)): + x = str1[: i + 1] + if self.checkDivisible(str1, str2, x): + ans=x + return ans From 0151ea97ca4394ab884b4bafbac33b5e7123ec3b Mon Sep 17 00:00:00 2001 From: glowfi <86939591+glowfi@users.noreply.github.com> Date: Wed, 7 Jun 2023 23:59:48 +0530 Subject: [PATCH 2/2] Create 0054-spiral-matrix-II.py --- python/0054-spiral-matrix-II.py | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 python/0054-spiral-matrix-II.py diff --git a/python/0054-spiral-matrix-II.py b/python/0054-spiral-matrix-II.py new file mode 100644 index 000000000..f6f3f4870 --- /dev/null +++ b/python/0054-spiral-matrix-II.py @@ -0,0 +1,40 @@ +class Solution: + def generateMatrix(self, n: int) -> List[List[int]]: + + ans=[[0 for _ in range(n)] for _ in range(n)] + + top = 0 + left = 0 + right = len(ans[0])-1 + bottom = len(ans)-1 + + number=1 + + while left <=right and top <=bottom: + # Left to Right + for i in range(left, right+1): + ans[top][i]=number + number+=1 + top += 1 + + # Top to Bottom + for i in range(top, bottom+1): + ans[i][right]=number + number+=1 + right -= 1 + + # Right to Left + if top <=bottom: + for i in range(right, left - 1, -1): + ans[bottom][i]=number + number+=1 + bottom -= 1 + + # Bottom to Top + if left <=right: + for i in range(bottom, top - 1, -1): + ans[i][left]=number + number+=1 + left += 1 + + return ans