Join the Non-Empty Parts
parts is a list of strings and sep is a separator string. Return the single string made by writing the non-empty entries of parts in their original order with sep between neighbouring entries.
Entries equal to the empty string "" are skipped entirely, so they never produce an extra separator. If every entry is empty, or parts has no entries, return "".
Example 1
parts ["a","","b","c"]
sep "-"
returns "a-b-c"
The empty entry is skipped, so a, b and c are joined with a single - between each pair.
Example 2
parts ["","x",""]
sep ", "
returns "x"
Only x is non-empty, so there is nothing to put a separator between.
Constraints
0 <= len(parts) <= 200 <= len(parts[i]) <= 80 <= len(sep) <= 3- entries have lowercase letters and digits only;
sephas spaces and punctuation only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Longest Vowel RunBeginner
- Longest Word in a SentenceBeginner
- Maximum Nesting Depth of ParenthesesBeginner
- Mirror CipherBeginner
- One Segment of OnesBeginner
- Percentage of Letter in StringBeginner
- Prefix of Any Word in a SentenceBeginner
- Remove Vowels from a StringBeginner