Wednesday, 26 July 2017

CTCI Challange, DAY 2

Problem Statement:- Strings: Making Anagrams

One good thing was i solved these strings related problems before in hackerrank, so I was confident before starting itself.

Start point:-
Problem was easy,I knew Just using the maps and the for loop it can be solved.

On Paper:-


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Making_Anagrams {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String a = scan.nextLine();
        String b = scan.nextLine();
        Map<Character, Integer> aMap = new HashMap<>();
        Map<Character, Integer> bMap = new HashMap<>();
        for (int i = 0; i < a.length(); i++) {
            if (aMap.containsKey(a.charAt(i))) {
                int count = aMap.get(a.charAt(i));
                count++;
                aMap.put(a.charAt(i), count);
            } else {
                aMap.put(a.charAt(i), 1);
            }
        }
        for (int i = 0; i < b.length(); i++) {
            if (bMap.containsKey(b.charAt(i))) {
                int count = bMap.get(b.charAt(i));
                count++;
                bMap.put(b.charAt(i), count);
            } else {
                bMap.put(b.charAt(i), 1);
            }
        }
        String temp = a;
        int charsToDelete = 0;
        for (int i = 0; i < temp.length(); i++) {
            if (aMap.containsKey(temp.charAt(i)) && bMap.containsKey(temp.charAt(i))) {
                if (aMap.get(temp.charAt(i)) != bMap.get(temp.charAt(i))) {
                    charsToDelete += Math.abs((aMap.get(temp.charAt(i)) - bMap.get(temp.charAt(i))));
                    aMap.put(temp.charAt(i), 0); //if this is not setted here then it will be recalculated for the next string also.
                    bMap.put(temp.charAt(i), 0);
                }
            } else {
                charsToDelete += aMap.containsKey(temp.charAt(i)) ? aMap.get(temp.charAt(i)) : bMap.get(temp.charAt(i));
                aMap.put(temp.charAt(i), 0);
                bMap.put(temp.charAt(i), 0);
            }
        }
        temp = b;
        for (int i = 0; i < temp.length(); i++) {
            if (aMap.containsKey(temp.charAt(i)) && bMap.containsKey(temp.charAt(i))) {
                if (aMap.get(temp.charAt(i)) != bMap.get(temp.charAt(i))) {
                    charsToDelete += Math.abs((aMap.get(temp.charAt(i)) - bMap.get(temp.charAt(i))));
                    aMap.put(temp.charAt(i), 0);
                    bMap.put(temp.charAt(i), 0);
                }
            } else {
                charsToDelete += aMap.containsKey(temp.charAt(i)) ? aMap.get(temp.charAt(i)) :                bMap.get(temp.charAt(i));
                aMap.put(temp.charAt(i), 0);
                bMap.put(temp.charAt(i), 0);
            }
        }
        System.out.println(charsToDelete);
    }
}

Mistakes:-
1. I failed to maintain the modularity,I was in a hurry(take care next time!)
2. Naming of variables should be still more elaborate.
3. The code on paper should be complete,just a psudocode is not enough.

No comments:

Post a Comment

Installing Docker and Minikube

  install docker-   sudo apt install docker.io   set user to docker group:- sudo gpasswd -a   {user} docker   imp commands:- ...