Encode & Decode Strings

Problem Summary

(Write in your own words, not copied from LeetCode. This forces comprehension.)

Key Observations

(Patterns, constraints, or hints in the problem statement.)

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class Solution:
    # 4%neet10%aaabbbcccd
    # len(word) + delimiter

    delimiter = "$"

    def encode(self, strs: List[str]) -> str:
        result = []
        for word in strs:
            result.append(str(len(word)))
            result.append(self.delimiter)
            result.append(word)
        return "".join(result)

    def decode(self, s: str) -> List[str]:
        result = []
        i = 0
        number = []
        while i < len(s):
            character = s[i]
            if character == self.delimiter:
                numOfChars = int("".join(number))
                startOfWord = i + 1
                endOfWord = startOfWord + numOfChars
                word = s[startOfWord:endOfWord]
                i = endOfWord
                number.clear()
                result.append(word)
            else:
                number.append(character)
                i += 1
        return result

Common Mistakes / Things I Got Stuck On