67.3k views
5 votes
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|1bcdef23423bc32Output: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|saOutput: 4|4|2|0|10Code 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}}

User Trinca
by
3.0k points

1 Answer

6 votes

Answer:

The code is below

Step-by-step explanation:

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

int sum = 0;

String arrblobs[] = blobs.split("\\|");

for (int i = 0; i < arrblobs.length; ++i) {

int answer = 0, index = 0;

for (;;) {

int position = arrblobs[i].indexOf(pattern, index);

if (position < 0)

break;

answer++;

index = position + 1;

}

System.out.print(answer + "|");

sum = sum + answer;

}

System.out.println(sum);

}

}

User Nijas
by
3.4k points