Saturday, November 23, 2019

How To Count A Lay Out Of Words Inwards Given String Inwards Java?

Can yous write a method inwards Java which accepts a String declaration as well as returns a release of words inwards it? H5N1 give-and-take is a sequence of 1 or to a greater extent than non-space grapheme i.e. whatever grapheme other than '' (empty String). This should hold upward your method signature:

public int count(String word);

This method should supply 1 if the input is "Java" as well as supply iii if the input is "Java, C++, Python". Similarly a telephone telephone to wordCount("    ") should supply 0.  This is 1 of the several String algorithmic questions yous tin await inwards a programming project interview.  This is used to exam the coding skills of the candidate as well as that's why it's really of import to laid for these questions anytime yous overstep for an interview.

Many programmers conduct hold personal collections of such questions which they revise every fourth dimension they overstep for a Programming project interview, but if yous don't conduct hold whatever yet, hence don't worry. You tin e'er piece of work the Cracking the Coding Interview, it contains 190 programming questions as well as solutions on every of import topic e.g. String, array, linked list, hash table, binary tree as well as other information structures.



Solution - Counting words inwards String

The solution of this work is really unproblematic if yous know a lilliputian flake of regular appear as well as how to split upward String inwards Java using regex (see here). Since work says that words are separated past times white space, which could hold upward infinite or tabs. So if yous split upward the String past times whitespace every bit shown here, yous tin acquire an array of String which is zilch but words.  Now the length of this array is your release of words, precisely supply that.




Though, yous demand to hold upward a lilliputian flake careful because in that place is likewise a possibility of to a greater extent than than 1 infinite betwixt ii words. So precisely using a regular appear to pick out handle of infinite or tab volition non hold upward enough, yous demand to piece of work a greedy regular expression to pick out handle of multiple spaces every bit well.

In Java, yous tin piece of work the regular appear designing "\\s+" to pick out handle of multiple spaces. The "\s" is a grapheme shape to abide by white space, which could check both infinite as well as tabs as well as "+" makes it greedy because it volition check 1 or to a greater extent than of "\s" designing i.e. 1 or to a greater extent than space. Now, since yous demand to escape the "\"  backward slash inwards Java, the whole designing becomes "\\s+". Se Mastering regular expression to acquire to a greater extent than virtually regex inwards Java.

 Can yous write a method inwards Java which accepts a String declaration as well as returns a release of wo How to count a release of words inwards given String inwards Java?



Java Program to count release of words inwards String

/**  * Java Program to count release of words inwards a given  * sentence. Words are separated past times whitespace inwards  * String.  * @author WINDOWS 8  *  */ public class StringCounter {       public static void main(String[] args) {                  // testing amongst non-empty String         String input = "Java is great";         int numOfWords = count(input);                  System.out.println("input: " + input);         System.out.println("count of words: " + numOfWords);                  // testing amongst empty String         input = "";         numOfWords = count(input);                  System.out.println("input: " + input);         System.out.println("count of words: " + numOfWords);                  // testing amongst zero String         input = null;         numOfWords = count(input);                  System.out.println("input: " + input);         System.out.println("count of words: " + numOfWords);      }          /**      * Return the release of words inwards given String      * @param sentence, words are separated past times whitespace      * @return count of words inwards judgement      */     public static int count(String sentence){         if(sentence == null || sentence.isEmpty()){             return 0;         }                  String[] words = sentence.split("\\s+");         return words.length;     }   }  Output: input: Java is swell count of words: iii input:  count of words: 0 input: null count of words: 0


That's all virtually how to count a release of words inwards a given String inwards Java. The regular appear play a joke on actually solves this work inwards a yoke of lines, but every bit a challenge, tin yous solve this work without using regular expression? If yous maintain existent interview, that would hold upward for certain a follo-up questions given how tardily the split() method solves this problem. So, why non travail it now? You tin likewise cheque the solution here, in 1 trial yous conduct hold tried as well as solved it yourself.


Other String coding Interview questiosns from Programming Interviews:
  • How to opposite String inwards Java without StirngBuffer? (solution)
  • How to count vowels as well as consonants inwards given String? (solution)
  • How to abide by duplicate characters on String? (solution)
  • How to cheque if a Stirng is palindrome inwards Java? (solution)
  • How to opposite words inwards a given String inwards Java? (solution)
  • How to impress all permutations of a String using recursion? (solution)

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2

No comments:

Post a Comment