Skip to content

Instantly share code, notes, and snippets.

View samarthsewlani's full-sized avatar

Samarth Sewlani samarthsewlani

View GitHub Profile
@samarthsewlani
samarthsewlani / Alternating Characters.py
Created February 21, 2021 08:07
Alternating Characters HackerRank Solution
def alternatingCharacters(s):
count=0
for i in range(1,len(s)): # We start from 1 as there is no previous charcter for 0th index
if s[i]==s[i-1]: # If current character is same as previous charcter
count+=1
return count
@samarthsewlani
samarthsewlani / Super Reduced String 1.py
Created February 21, 2021 09:01
Super Reduced String HackerRank Solution ( Brute Force )
def superReducedString(s):
currentstring=""
nextstring=s
while currentstring!=nextstring: #Keep on doing this till there are no deletions,currentstring and nextstring would be same if there are no deletions
currentstring=nextstring
nextstring=""
length,i=len(currentstring),0
while i<length:
if i<length-1: #This if condition is necessary because if i is on last letter, accessing i+1 from string would result into IndexError
if currentstring[i]==currentstring[i+1]:
@samarthsewlani
samarthsewlani / Super Reduced String 2.py
Created February 21, 2021 09:06
Super Reduced String HackerRank Solution ( Better logic )
class Node: #Definition of Node class
def __init__(self,value,previous):
self.value=value
self.next=None
self.prev=previous
def __str__(self):
return "Node {}".format(self.value)
def superReducedString(s):
@samarthsewlani
samarthsewlani / Super Reduced String 3.py
Created February 21, 2021 09:07
Super Reduced String HackerRank Solution ( Efficient )
def superReducedString(s):
stack=["#"] #Initializing the stack with special character because if there are no elements and we access the top of stack it results into IndexError
for i in s:
if stack[-1]==i: #If the element is same as top of stack
stack.pop() #Pop the last element and do not push current element
else:
stack.append(i) #Else push the element in the stack
ans=""
for i in stack[1:]: #Ignoring the first index
ans+=i #Store the answer in the variable 'ans'
@samarthsewlani
samarthsewlani / Truck Tour 1.py
Created February 21, 2021 09:22
Truck Tour HackerRank Problem
def solve(petrol,distance,n):
start,tank=0,0
for i in range(n): #Start from each index
start=i
tank=0 #Reset the tank for each iteration
for j in range(n):
current=(start+j)%n #To make the index in range for circular array
tank+=petrol[current]-distance[current] #Add the petrol and subtract the petrol required to reach next station
if tank<0: #If petrol in tank becomes negative, we cannot complete the loop
break
@samarthsewlani
samarthsewlani / Truck Tour 2.py
Created February 21, 2021 09:23
Truck Tour HackerRank Solution
n=int(input())
petrol,distance=[],[]
for i in range(n):
p,d=[int(x) for x in input().split()]
petrol.append(p),distance.append(d)
start,xsum=0,0
for i in range(n):
xsum+=petrol[i]-distance[i] #Calculating the difference and adding to the sum
if xsum<0: #If sum becomes negative, that means we cannot complete the loop
start=i+1 #So start from the next index
@samarthsewlani
samarthsewlani / Valid Parentheses.py
Created February 22, 2021 07:58
Valid Parentheses LeetCode Solution
class Solution:
def isValid(self, s: str) -> bool:
stack=["#"] # Initializing with a dummy character
for i in range(len(s)):
if s[i]=="(" or s[i]=="{" or s[i]=="[": # If opening bracket, push in stack
stack.append(s[i])
continue
if (s[i]==")" and stack[-1]=="(") or (s[i]=="}" and stack[-1]=="{") or (s[i]=="]" and stack[-1]=="["):
stack.pop()
else:
@samarthsewlani
samarthsewlani / Strange Counter.py
Created February 22, 2021 08:08
Strange Counter HackerRank Solution
def strangeCounter(t):
end,size,cycle=3,3,1
while end<t:
size=2*size
end+=size
cycle+=1
return end-t+1
@samarthsewlani
samarthsewlani / Minimum Distances 1.py
Created February 22, 2021 08:25
Minimum Distances HackerRank Solution ( Brute Force )
def minimumDistances(a):
n=len(a)
minimum=n+1 #Initializing with n+1 because if no updates take place we should return -1 as there are no matching pairs
# Note:- If any matching pairs is found the distance would be less than n
for i in range(n-1):
for j in range(i+1,n):
if a[i]==a[j]:
if j-i<minimum:
minimum=j-i
if minimum==n+1:
@samarthsewlani
samarthsewlani / Minimum Distances 2.py
Last active February 22, 2021 08:28
Minimum Distances HackerRank Solution ( Better Logic )
def minimumDistances(a):
n=len(a)
minimum=n+1 #Initializing with n+1 because if no updates take place we should return -1 as there are no matching pairs
# Note:- If any matching pairs is found the distance would be less than n
indexes=dict() #Creating an empty dictionary
for i in range(n):
if a[i] in indexes: #If elem is present, append its index in its list
indexes[a[i]].append(i)
else: #If elem is not present,initialize it by a list with t's index
indexes[a[i]]=[i]