Delimiting Spaces 1 (with.Java)
This is the first installment of our series on Space Separator 1.
We’re going to be solving coding test problems, reflecting on the problems we’ve solved, and learning about other ways to solve them.
Let’s start with the problem
Problem
Given a string my_string with words separated by a single space as a parameter, write a solution function that returns an array of strings containing the words in my_string, ordered from front to back.
Example input and output
my_string | result |
---|---|
“i love you” | [“i”, “love”, “you”] |
“programmers” | [“programmers”] |
My solution to the problem
class Solution {
public String[] solution(String my_string) {
String[] answer = my_string.split(" ");
return answer;
}
}
Solution Explained
Solved by using the SPLIT function to sequentially store spaces as separators in an array of strings to match the conditions of the problem.