Revisiting JAVA Map – Make a ransom note out of a Magazine

Write a java program to figure out if a ransom note (string) can be formed from a given magazine (string).. Shall we? RansomNoteWriter0103

import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;

public class Main {

public static void main(String[] args){

String ransomNote = “I miss the good old days of programming for fun and not for money”;
String magazineString = “ld programming not for miss I money the and good days of for fun”;

if(ransomNote.length()> magazineString.length())
{
System.out.print(“Oops!! Looks like you need some more letters to prepare your note!”);
}
else
{
Map ransomNoteCounter = charCount(ransomNote);
Map mgznNotecounter = charCount(magazineString);
int flag= 1;

outerloop:
for(Object key:ransomNoteCounter.keySet())
{
if(!(mgznNotecounter.containsKey(key) && ransomNoteCounter.get(key)== mgznNotecounter.get(key))){
System.out.println(“Oops!! Looks like you need some more letters to prepare your note!”);
flag =0;
break outerloop;
}

}
if(!(flag==0)) {
System.out.println(“Yeyy! You’re ready to send the note!”);
}
}

}

public static Map charCount(String charstring){

char[] char_array =charstring.toCharArray();
char ch = char_array[0];
Map charCounter=new HashMap();
for(int i=0;i<charstring.length();i++)
{
ch = char_array[i];
if(charCounter.containsKey(ch))
{
charCounter.put(ch, charCounter.get(ch)+1);
}
else
{
charCounter.put(ch, 1);
}
}

return charCounter;

}

}

Advertisements

One thought on “Revisiting JAVA Map – Make a ransom note out of a Magazine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s