Question: Given a pattern as the first argument and a string of blobs split by | show the number of times the pattern is present in

Given a pattern as the first argument and a string of blobs split by | show the number of times the pattern is present in each blob and the total number of matches.

Input:

The input consists of the pattern ("bc" in the example) which is separated by a semicolon followed by a list of blobs ("bcdefbcbebc|abcdebcfgsdf|cbdbesfbcy|1bcdef23423bc32" in the example). Example input: bc;bcdefbcbebc|abcdebcfgsdf|cbdbesfbcy|1bcdef23423bc32

Output:

The output should consist of the number of occurrences of the pattern per blob (separated by |). Additionally, the final entry should be the summation of all the occurrences (also separated by |). Example output: 3|2|1|2|8 where bc was repeated 3 times, 2 times, 1 time, 2 times in the 4 blobs passed in. And 8 is the summation of all the occurrences. (3+2+1+2 = 8)

Test 1:

Input: aa;aaaakjlhaa|aaadsaaa|easaaad|sa

Output: 4|4|2|0|10

Code to be used:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.nio.charset.StandardCharsets;

public class Main {

/**

   * Iterate through each line of input.

   */

public static void main(String[] args) throws IOException {

InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);

BufferedReader in = new BufferedReader(reader);

String line;

while ((line = in.readLine()) != null) {

String[] splittedInput = line.split(";");

String pattern = splittedInput[0];

String blobs = splittedInput[1];

Main.doSomething(pattern, blobs);

}

}

  

public static void doSomething(String pattern, String blobs) {

// Write your code here. Feel free to create more methods and/or classes

}

}

Step by Step Solution

3.48 Rating (155 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javaioBufferedReader import javaioIOException import javaioInputStreamReader import ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!

Related Book