I am working on solving the code challenge from the link below but it fails from cases 13 to 19. I am reading that HashMap can take a large amount of data but my solution seems is not working for the cases when the array (magazine) is of 30K.
Below is the solution I implemented.
Help is appreciated.
I tested the code with arrays of 4K and it works fine
static void checkMagazine(String[] magazine, String[] note) {
int initSize_m = (int) Math.ceil(magazine.length / 0.75);
int initSize_n = (int) Math.ceil(note.length / 0.75);
HashMap<Integer, String> m_hash= new HashMap<Integer, String>(initSize_m);
HashMap<Integer, String> n_hash= new HashMap<Integer, String>(initSize_n);
for(int i= 0; i < note.length; i++){
n_hash.put(i, note[i]);
}
for(int i= 0; i < magazine.length; i++){
m_hash.put(i, magazine[i]);
}
boolean flag=true;
if(note.length<magazine.length){
for (Map.Entry<Integer, String> entry : n_hash.entrySet()) {
flag = m_hash.containsValue(entry.getValue());
if(flag){
m_hash.values().removeIf(v -> v.equals(entry.getValue()));
}else{
break;
}
}
}else{
for (Map.Entry<Integer, String> entry : m_hash.entrySet()) {
flag = n_hash.containsValue(entry.getValue());
if(flag){
n_hash.values().removeIf(v -> v.equals(entry.getValue()));
}else{
break;
}
}
}
if(flag){
System.out.println("Yes");
}else{
System.out.print("No");
}
}
Cases from 13 to 19 fail