P17982 [GeeksforGeeks] Count number of words1500
普及-
题目描述

Given a string consisting of $spaces,\t,\n$ and lower case alphabets.Your task is to count the number of words where $spaces,\t$ and $\n$ work as separators.

Example 1:
Input: S = "abc def"
Output: 2
Explanation: There is a space at 4th
position which works as a seperator
between "abc" and "def"

Example 2:
Input: S = "$a\nyo\n$"
Output: 2
Explanation: There are two words "a"
and "yo" which are seperated by "$\n$".
Your Task:
You don't need to read input or print anything. Your task is to complete the function countWords() which accepts a string as input and returns number of words present in it.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
where N is length of given String.
Constraints:
2 <= Length of String <= 10^{6}

输入格式

见题面

输出格式

见题面

样例输入
S = "abc def"
样例输出
2