String Concatenation Versus Join
Both functions build the same report string from a list of rows:
class ReportBuilder:
def __init__(self):
self.text = ""
def add_line(self, line: str) -> None:
self.text = self.text + line + "\n"
def build_report_v1(rows: list[str]) -> str:
builder = ReportBuilder()
for row in rows:
builder.add_line(row)
return builder.text
def build_report_v2(rows: list[str]) -> str:
return "".join(row + "\n" for row in rows)
For n = len(rows), what is the total time complexity of each function, and why?
Sign in to answer questions and track your progress
Sign In