Just for fun I have been trying to solve an algorithm challenge but sadly my solution fails. Here is the description of the challenge:
A quick solution I came up with is the following:Input
Each test file contains three lines.
First line starts with two space-separated integers NN and MM, the size of the list and the number of words Kanga says respectively.
Second line follows, containing NN space-separated words. This composes the no-no list.
Third line contains MM space-separated words. This composes the words Kanga has said during the day.
Each word is always strictly composed of lowercase english characters (a-z).
All words in the no-no list are unique, i.e. there are no repeated words
The length of each word is at most 1010 characters long.
Output
A single line containing a number - the number of times Kanga said a word that was in the no-no list.
Example:
Sample Input:
5 12
smallest take most room in
sometimes the smallest things take up the most room in your heart
Sample Output:
5
The solution above "seems" to solve the problem but whenever I submit it it always fails. Can anyone point out where I could be failing their tests?Code:import java.util.Scanner; public class JavaApplication1 { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); //System.out.print("Input N & M: "); String NM = myObj.nextLine(); //System.out.print("Input N: "); //String N = "smallest take most room in"; String N = myObj.nextLine(); String N2 = N.replace(" ", "XX"); //System.out.print("Input M: "); //String M = "sometimes the smallest things take up the most room in your heart"; //myObj.nextLine(); String M = myObj.nextLine(); String[] tempArray; String delimiter = " "; tempArray = M.split(delimiter); int occurence = 0; for (int i = 0; i < tempArray.length; i++) { if (N2.contains(tempArray[i])) occurence += 1; } System.out.println(occurence); } }




Reply With Quote