Python Solution without using reversed() or trim() or split()


  • 0
    J
    class Solution(object):
        def reverseWords(self, s):
            """
            :type s: str
            :rtype: str
            """
            rightIndex = leftIndex = len(s)-1
            reverseString = ''
            while leftIndex >= 0:
                if s[leftIndex]==' ':
                    numOfSpaces = self.countSpaces(s,leftIndex)
                    if s[rightIndex]!=' ':
                        reverseString += (s[leftIndex+1:rightIndex+1]+' ')
                    leftIndex -= numOfSpaces
                    rightIndex = leftIndex
                else:
                    leftIndex -= 1
    
            reverseString += (s[leftIndex+1:rightIndex + 1])
            if reverseString and reverseString[0]== ' ':
                reverseString = reverseString[1:]
            if reverseString and reverseString[-1] == ' ':
                reverseString = reverseString[:-1]
            return reverseString
    
        def countSpaces(self, s, index):
            count = 0
            i = -(len(s)-index)
            for c in s[i::-1]:
                if c != ' ':
                    break
                else:
                    count +=1
            return count
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.