Skip to content

Commit

Permalink
Add DSA problems
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddharthaMishra-dev committed Jan 30, 2023
1 parent f3af93a commit 98e92bd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
28 changes: 28 additions & 0 deletions longestprefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
arr=['hello','world','hell']
minword=min(arr)
print(minword)
prefix=""
for i in arr:
temp=""
for j in range(len(minword)):
if i[j] == minword[j]:
temp+=minword[j]
if prefix=="":
prefix=temp
if len(prefix)>len(temp):
prefix=temp

flag=1
for i in arr:
if prefix[0] != i[0]:
flag=0
break

if flag==1:
print(prefix)
else:
print("no common element")




16 changes: 16 additions & 0 deletions nextgreaterelement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
arr=[1,3,2,4]
n=len(arr)
stack=[]
result=[0]*n
for i in range(n-1,-1,-1):
while stack and stack[-1]<=arr[i]:
stack.pop()
if stack:
result[i]=stack[-1]
stack.append(arr[i])

for i in range(n):
if result[i]==0:
result[i]=-1

print(result)
8 changes: 8 additions & 0 deletions strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
s1="siddhartha"
s2='siddhantha'
temp=[]
for i in s1:
if i not in temp:
temp.append(i)

print(temp)
18 changes: 18 additions & 0 deletions trappingrainwater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
arr=[3,1,2,4,0,1,3,2]
n=len(arr)
left=[]
right=[0]*n
left.append(arr[0])
for i in range(1,n):
left.append(max(arr[i],left[i-1]))

print(left)
right[n-1]=arr[n-1]
for i in range(n-2,-1,-1):
right[i]=max(right[i+1],arr[i])


total=0
for i in range(0,n):
total=total+(min(left[i],right[i])-arr[i])
print(total)

0 comments on commit 98e92bd

Please sign in to comment.