Created
February 21, 2021 09:01
-
-
Save samarthsewlani/731dfc0eb6a36e7309cd7f4ae82f43fe to your computer and use it in GitHub Desktop.
Super Reduced String HackerRank Solution ( Brute Force )
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]: | |
i+=1 #Skipping characters if they are same | |
else: | |
nextstring+=currentstring[i] #Appending characters to nextstring if they are not same | |
else: | |
nextstring+=currentstring[i] #Last letter cannot be checked so directly append it to nextstring | |
i+=1 | |
if nextstring: #If nextstring is not empty return it | |
return nextstring | |
return "Empty String" #else return the string "Empty String" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment