Thursday, October 20, 2011

String : Anagrams or not

Question : Find if two words are anagrams or not.

Solution :
The best solution basically involves sorting the characters in the words and then comparing to check if they are same.

public static boolean isAnagram(String s1, String s2){
        if (s1.length() != s2.length() )
            return false;
        char s1array[] = s1.toLowerCase().toCharArray();
        char s2array[] = s2.toLowerCase().toCharArray();
        for (int i=0;i <s1.length();i++)
            if (s1array[i] != s2array[i])
                return false;
        return true;

String : Remove duplicate characters

Question : Remove Duplicate characters from a string.

Solution :

Basically the solution involves counting the occurences of characters in the string .

First solution uses an in-built Hashtable :

    public static String removeDuplicateCharHash(String ip){
        Hashtable<Character, Integer> ht = new Hashtable<Character, Integer>();
        String op = new String("");
        for(int i=0;i<ip.length();i++){
            Character ch = ip.charAt(i);
            if (! ht.containsKey(ch))
            { op = op + ch; ht.put(ch, new Integer(1)); }
        return op;
Second Solution uses a bit vector which is used as a hastable:

    public static String removeDuplicateChar(String ip){
// Initialize the bit vector  
    int ht[] = new int[256];
        for (int i=0;i<256;i++)
            ht[i] = 0;
  String op = "";
        for (int i=0;i<ip.length();i++){
            char ch = ip.charAt(i);
            if (ht[ch] == 0)
            {op = op + ch; ht[ch]++;}
        return op;

Monday, October 3, 2011

Increase EBS Image Size

Increase EBS Image Size

Scenario : Lets say you wanted to make your own image based on the stock Amazon AMI. But after you are done with the painful customization , you realize you started of with a EBS size of 6GB !
You need more than that !

You would have 2 options now :
1. Restart with a bigger size AMI. (Good luck and have fun again.)
2. Resize the current one to a bigger size.

This post is about the second option.

Assuming your current instance name is INST1, the attached volume is VOL1SMALL.
It can be an easy task if you follow the steps shown below :

1. Stop the instance. (I have noticed the steps below complete a lot faster if the instance is stopped)
2. Create a snapshot of the attached volume . Let it be SNAP1.
3. Now create a new volume from the snapshot you just created. Let this be VOL2BIG (Choose your higher size while doing this )
4. Select the already attached volume, i.e. VOL1SMALL and notice the path where it is mounted. It should be most probably /dev/sda1 . Now Detach this volume.
5. Now right click on the newly created volume VOL2BIG and set the mount path to be the same "/dev/sda1".
6. Start the instance again. INST1.
7. Do the df -h and you'll STILL SEE 6GB ! What the Hell !! Don't panic :-)

All you gotta do is resize the partition. To do that :

1. run "fdisk -l"
Note the device name of the newly attached volume. In my case it was "/dev/xvde1"

2. run "resize2fs /dev/xvde1 SIZE"

Note SIZE should include the units as well. If you want it to grow to 15Gigs .. use 15G
Do man resize2fs for more details.

Thats it ... Running df -h will now show you the new size !

Enjoy !