Funcia
Tutorialarraystring

Join Words with a Separator

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
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

wordssepresult
["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

  • 1≤1 \le words.length ≤100\le 100
  • Each word has 1 to 20 lowercase English letters.
  • 1≤1 \le length of sep ≤3\le 3, made of the characters -, ,, /, +, _ and space.

Expected complexity

Time
O(total length)
Space
O(N)

Examples

wordssepExpected
["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

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1200 msestimated
C# 14 (.NET 10)1200 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness10010verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.