
Problem Statement
The task is to determine the total number of segments in a provided string s
. A segment is defined as any contiguous subsequence within the string that does not contain any space characters and is isolated by spaces or the boundaries of the string. For instance, in the string "Hello, my name is John", the segments are: "Hello,", "my", "name", "is", "John". Each segment is composed exclusively of non-space characters, and their count amounts to the desired output.
Examples
Example 1
Input:
s = "Hello, my name is John"
Output:
5
Explanation:
The five segments are ["Hello,", "my", "name", "is", "John"]
Example 2
Input:
s = "Hello"
Output:
1
Constraints
0 <= s.length <= 300
s
consists of lowercase and uppercase English letters, digits, or one of the following characters"!@#$%^&*()_+-=',.:"
.- The only space character in
s
is' '
.
Approach and Intuition
To solve this problem, we'll break down the steps required to count the number of segments in a string, ensuring we understand the input structure and constraints:
- Initialization: Start with a counter set to zero to keep track of segments.
- Traverse the String: Iterate through each character in the string:
- Identify if a character marks the beginning of a new segment. This can be detected if it's not a space character and either it's the first character of the string or the character before it was a space.
- If the above condition is true, increment the segment counter.
- Edge Case Handling: Consider special cases where:
- The string is empty; the count should obviously be zero.
- The string consists only of spaces, where again, the count should be zero.
Example Walkthroughs:
For the input
"Hello, my name is John"
:- Start with the first character 'H' (non-space), identify it as the start of a new segment, increment the counter.
- Proceed until a space is encountered, then move to the next non-space character 'm', recognizing another new segment, and so forth until the end of the string.
- Total count equals five corresponding to each of the words or separated character sequences.
For the input
"Hello"
:- Only one contiguous block of non-space characters is found, so the total segment count is one.
Understanding these steps will collectively help in implementing a solution that addresses the problem constraints effectively: a maximum string length of 300 characters, the inclusion of various characters besides alphabets, and handling string inputs carefully to account for various edge scenarios described.
Solutions
- Java
class Solution {
public int countWordsInString(String input) {
int wordCount = 0;
for (int idx = 0; idx < input.length(); idx++) {
if ((idx == 0 || input.charAt(idx-1) == ' ') && input.charAt(idx) != ' ') {
wordCount++;
}
}
return wordCount;
}
}
The given Java program provides a method to calculate the number of segments (or words) in a given string. The method countWordsInString
initiates with a word count set to zero. It then iterates through each character in the string, checking:
- Start from the first character or any character following a space,
- Ensure that the current character is not a space.
Every time these conditions are met, they indicate the start of a new word, and therefore the word count is incremented. The method finally returns the total count of words detected in the input string. This solution efficiently counts words separated by spaces without using any built-in string splitting functions.
No comments yet.