Tutorialarraystring
Join Words with a Separator
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an array of words and a separator string sep, return the words concatenated in order with sep placed
only between neighbouring words (not at the start or end). A single word is returned as is.
Examples
| words | sep | result |
|---|---|---|
["apple", "banana", "cherry"] | ", " | "apple, banana, cherry" |
["solo"] | "-" | "solo" |
["a", "b", "c", "d"] | "+" | "a+b+c+d" |
Example 1 — A comma and a space between words.
Example 2 — A single word needs no separator.
Example 3 — a+b+c+d.
This is a tutorial problem: there are no performance tests, just return the correct value.
Function signature
solution(words: string[], sep: string) → string
Constraints
-
words.length - Each word has 1 to 20 lowercase English letters.
- length of
sep, made of the characters-,,,/,+,_and space.
Expected complexity
- Time
- O(total length)
- Space
- O(N)
Examples
| words | sep | Expected |
|---|---|---|
["apple","banana","cherry"] | ", " | "apple, banana, cherry" |
["solo"] | "-" | "solo" |
["a","b","c","d"] | "+" | "a+b+c+d" |
Hint
Hint
Start with words[0] and, for every following word, append sep and then the word; that way no separator is
left at the end. sep.join(words) in Python and String.join(sep, words) in Java do the same.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1200 ms | estimated |
| C# 14 (.NET 10) | 1200 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 100 | 10 | verdict only |