878737. Print Without Space

Basic

Print Without Space

Slug: print-without-space--124240

Difficulty: Basic

Topic Tags: python-io, CPP, Java

Summary

The problem is about printing a given string without spaces. This means that all characters in the string should be printed one after the other, without any space in between.

Detailed Explanation

To solve this problem, we can simply iterate through the input string and print each character. Since there are no spaces to handle, we don't need to worry about skipping over them or adjusting our printing logic in any way. The key concept involved here is the idea of iterating through a string and printing its contents.

Here's a step-by-step breakdown of the solution:

  1. Start by initializing an empty string result that will store the output.
  2. Iterate through the input string, character by character.
  3. For each character, append it to the result string.
  4. Finally, print the result string.

Time complexity: O(n), where n is the length of the input string. This is because we're iterating through the string once and performing a constant amount of work for each character.

Space complexity: O(1), since we're only using a constant amount of space to store the result string, regardless of the size of the input string.

Optimized Solutions

Java

java
public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        System.out.println(str.replace(" ", ""));
    }
}

Python

View on GeeksforGeeks

Not available

DevExCode

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

© 2026 DevExCode. All rights reserved.