Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions python/0059-spiral-matrix-II.py
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions python/1071-greatest-common-divisor-of-strings.py
Original file line number Diff line number Diff line change
@@ -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