Mediocre solution using Python


  • 0
    R

    This is not an elegant solution, but rather as it may come from a mediocre student during an actual interview timeframe. This is my first post in discuss, feedback is welcome.

    from collections import deque
    class Solution(object):
        def licenseKeyFormatting(self, S, K):
            """
            :type S: str
            :type K: int
            :rtype: str
            """
            to_return = deque()
            chunk_size = 0
            for i in range(len(S)-1, -1, -1):
                c = S[i]
                if c == '-':
                    continue
                else:
                    if chunk_size < K:
                        to_return.appendleft(self._uppercase(c))
                        chunk_size += 1
                    else:
                        to_return.appendleft("-")
                        to_return.appendleft(self._uppercase(c))
                        chunk_size = 1
            return ''.join(to_return)
    
        def _uppercase(self, c):
            if ord('a') <= ord(c) and ord(c) <= ord('z'):
                return c.upper()
            else:
                return c
    

Log in to reply
 

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