878737. Print Without Space
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:
- Start by initializing an empty string
resultthat will store the output. - Iterate through the input string, character by character.
- For each character, append it to the
resultstring. - Finally, print the
resultstring.
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
Not available