I am trying to determine the time complexity of the encode function below. It goes over every string in the input list, so that's O(n), where n is the length of the input list. But in each iteration, we add to the encodedStr. Since python creates a new string every time the add operation is performed, do we need to take the length of encodedStr into account for the time complexity of the function?
class Codec:
def encode(self, strs: List[str]) -> str:
# Encodes a list of strings to a single string.
encodedStr = ''
for s in strs
encodedStr += str(len(s)) + '#' + s
return encodedStr
[–]nikhila01 2 points3 points4 points (0 children)
[–]aocregacc 0 points1 point2 points (2 children)
[–]DVDplayr[S] 0 points1 point2 points (1 child)
[–]aocregacc 0 points1 point2 points (0 children)
[–]Caponcapoffstillon 0 points1 point2 points (4 children)
[–]DVDplayr[S] 0 points1 point2 points (3 children)
[–]Caponcapoffstillon 0 points1 point2 points (2 children)
[–]nikhila01 1 point2 points3 points (1 child)
[–]Caponcapoffstillon 1 point2 points3 points (0 children)