Skip to content

Latest commit

 

History

History
59 lines (48 loc) · 1.02 KB

2390. Removing Stars From a String.md

File metadata and controls

59 lines (48 loc) · 1.02 KB

Approach 1

class Solution:
    def removeStars(self, s: str) -> str:

        s=list(s)
        n=s.count('*')

        for i in range(n):
            j=s.index('*')
            if j==0:
                s.pop(j)
            elif s[j-1]!='*':
                s.pop(j)
                s.pop(j-1)
            else:
                s.pop(j)
        
        res=''
        for i in s:
            res=res+i

        return res

Exceeding Time Limit

Solution 1

class Solution:
    def removeStars(self, s: str) -> str:

        n=s.count('*')
        for i in range(n):
            j=s.index('*')
            if j==0:
                s=s[:j]+s[j+1:]
            elif s[j-1]!='*':
                s=s[:j-1]+s[j+1:]
            else:
                s=s[:j]+s[j+1:]
        
        return s

Approach 2

class Solution:
    def removeStars(self, s: str) -> str:
        
        l=''
        for i in s:
            if i=='*':
                l=l[:-1]
            else:
                l=l+i
        
        return l