This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def strangeCounter(t): | |
end,size,cycle=3,3,1 | |
while end<t: | |
size=2*size | |
end+=size | |
cycle+=1 | |
return end-t+1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
OlderNewer