Problem
Given a string, find number of words in that string.
For this problem a word is defined by a string of one or more english alphabets.
Input Format
A string not more than 400000 characters long.
The string can be defined by following regular expression:
[A-Za-z !,?.\_'@]+
That means the string will only contain english alphabets,
blank spaces and this characters: "!,?._'@".
Output Format
In the first line, print number of words nn in the string.
The words don't need to be unique. In the next nn lines,
print all the words you found in the order they appeared in the input.
Sample Input
He is a very very good boy, isn't he?
Sample Output
10
He
is
a
very
very
good
boy
isn
t
he
Solution
Solution
import java.io.*; import java.util.*;
public class Solution { public static void main(String[] args){ Scanner scan = new Scanner(System.in); String str = scan.nextLine().trim(); String delims = "[_\\@ !,?.']+"; int words = str.split(delims).length; if(str.length()==0) System.out.println("0"); else if(str.length()<=400000){ System.out.println(words); for(String word : str.split(delims)){ System.out.println(word); } } } }
Comments
Post a Comment