Copied to Clipboard
Count number of words
Easy Accuracy: 22.94% Submissions: 5521 Points: 2

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.

2 <= Length of String <= 106


We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Count number of words

Output Window