Concatenate One String to Another
Problem
Given two strings s1 and s2, return the string formed by appending s2 to the end of s1. Then handle the follow-ups: concatenate a list of n strings efficiently, and explain why naive repeated concatenation is slow in languages with immutable strings.
Input / Output
- Input: strings
s1,s2(or a list of strings). - Output: the concatenated string.
Examples
Example 1
Input: s1 = "Hello, ", s2 = "World"
Output: "Hello, World"
Example 2
Input: s1 = "", s2 = "abc"
Output: "abc"
Example 3 (list)
Input: ["a", "b", "c", "d"]
Output: "abcd"
Constraints
0 <= |s1|, |s2| <= 10^5; total length of a list<= 10^6.
Approach
Two strings. Allocate a buffer of size |s1| + |s2|, copy s1, then copy s2. In C this is strcat after ensuring the destination has room; in most high-level languages the + operator does exactly this in O(|s1| + |s2|).
Many strings — the trap. Writing result += s inside a loop copies the growing result every iteration in Java, Python, JavaScript, C#, etc., because strings are immutable there. n appends of length-L strings cost O(n²·L). The fix is a string builder (StringBuilder, ''.join(list), Array.join) which accumulates pieces and copies once at the end, giving O(total length).
Manual implementation without library help (what the interviewer may ask for): compute the total length, allocate a character array once, and copy each string into place with a running offset.
Complexity
- Time: O(total length)
- Space: O(total length) for the result.
Reference solution (Python)
def concat(s1: str, s2: str) -> str:
return s1 + s2
def concat_many(parts):
# manual builder: single allocation, single pass
buf = []
for p in parts:
buf.append(p)
return ''.join(buf)
Test cases
| Input | Expected |
|---|---|
"Hello, ", "World" | "Hello, World" |
"", "abc" | "abc" |
["a","b","c","d"] | "abcd" |
[] | "" |