➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Faulty Keyboard

image image

Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected.

You are given a 0-indexed string s, and you type each character of s using your faulty keyboard.

Return the final string that will be present on your laptop screen.

So my approach to solve this is to iterate through each character in the string and append them to a new list during the iteration when I find the character i I’ll remove the value of the last element in the list which would be i then reverse the list

My solve script is in the below link.

Solve Script: link image

Leetcode Submission Script

class Solution:
    def finalString(self, s: str) -> str:
        s = list(s)
        rev = []

        for i in range(len(s)):
            rev.append(s[i])
            if s[i] == 'i':
                rev.pop()
                rev = rev[::-1]        

        r = "".join(rev)

        return r