Tutorials Hut

Java Program To Find Possible Occurrence Of Word In Given String

We are going to write a Java Program to find possible occurrences of words in a given String.

Problem Statement

Program should be able to take a word as string and another word as raw data and should return how many times a word can be formed by using letters from raw data. Remember we can’t reuse the same letter again and again.

Example 1: Find how many “balloon” words can be formed from “baloonsss”

Answer is 1, there are all letters available even 2 o’s to form the word “balloon”

Example 2:  Find how many “balloon” words can be formed from “ballon”

Answer is 0, because there is only one “o” letter in raw string.

Example 3: Find how many “balloon” words can be formed from “balloonballoonnss”

Answer is 2.

Java Program To Find Possible Occurrence Of Word In Given String

package problems;

import java.util.HashMap;
public class FindOccurrenceOfWord {
/*
find how many times we can create balloon word from a given string, not we can only use once a given char.
balloon
b =1
a = 1
l = 2
o = 2
n = 1
*/

public static void main(String[] args) {
    System.out.println("for word 'ballo1oonsballnso' occurrence of 'balloon' is "+ maxNumberOfBalloons("ballo1oonsballnso"));
     System.out.println("for word 'balloons' occurrence of 'balloon' is "+ maxNumberOfBalloons("balloons"));

}

static public int maxNumberOfBalloons(String text) {
    //dict ready
     HashMap<String, Integer> charCountMap= new  HashMap<String, Integer>();

    for(int i=0; i < text.length(); i++) {
        String charStr = String.valueOf(text.charAt(i));
        if(charCountMap.get(charStr)!=null) {
            charCountMap.put(charStr, charCountMap.get(charStr) + 1);
        } else {
            charCountMap.put(charStr, 1);
        }
    }
    //check how many times things r in dict
    Boolean result = true;
    int count = 0;
    while(result) {
        int b =0;
        int a = 0;
        int l = 0;
        int o = 0;
        int n = 0;
        if(charCountMap.get("b")!=null) {
            b = 1;
            int tmp = charCountMap.get("b") - 1;
            if(tmp < 0 ) break;
            charCountMap.put("b", tmp);
        } else break;
        if(charCountMap.get("a")!=null) {
            a = 1;
               int tmp = charCountMap.get("a") - 1;
            if(tmp < 0 ) break;
            charCountMap.put("a", tmp);
        }else break;

        if(charCountMap.get("l")!=null) {
            l = 1;
            int tmp = charCountMap.get("l") - 2;
            if(tmp < 0 ) break;
            charCountMap.put("l", tmp)
         }else break;

        if(charCountMap.get("o")!=null) {
            o= 1;
            int tmp = charCountMap.get("o") - 2;
            if(tmp < 0 ) break;
            charCountMap.put("o", tmp);
         }else break;

        if(charCountMap.get("n")!=null) {
            n = 1;
            int tmp = charCountMap.get("n") - 1;
            if(tmp < 0 ) break;
            charCountMap.put("n", tmp);
         }else break;

         if(b > 0 && a >0 && l>0 && o>0 &&  n>0)  {

            count++;
        }
     }

    return count;
}
}

Output

for word 'ballo1oonsballnso' occurrence of 'balloon' is 2
for word 'balloons' occurrence of 'balloon' is 1

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *