878732. Print With Separator

Basic

Print With Separator

Summary

Given a list of strings, print them with a separator in between. The separator can be any string. This problem requires understanding of basic input-output operations and string manipulation.

Detailed Explanation

To solve this problem, we need to iterate over the list of strings and append the separator after each element except the last one. We'll use a loop to achieve this.

Here's the step-by-step breakdown:

  1. Initialize an empty string result that will store the final output.
  2. Iterate over the list of strings. For each string, append it to result.
  3. After appending each string, add the separator (if not at the last element).
  4. Finally, print the result.

Time complexity: O(n), where n is the number of elements in the input list. Space complexity: O(n), as we're storing the output in a string.

Optimized Solutions

Java

View on GeeksforGeeks
public class PrintWithSeparator {
    public static void print(String[] strings, String separator) {
        for (int i = 0; i < strings.length; i++) {
            System.out.print(strings[i]);
            if (i < strings.length - 1) {
                System.out.print(separator);
            }
        }
        System.out.println();
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.