Problem Statement

Input Format

The first line contains a single string, a. The second line contains a single string, b.


1<= |a|,|b| <= 10^4

It is guaranteed that and consist of lowercase English alphabetic letters (i.e., through ). Output Format

Print a single integer denoting the number of characters you must delete to make the two strings anagrams of each other.

Sample Input



Sample Output



We delete the following characters from our two strings to turn them into anagrams of each other:

Remove d and e from cde to get c. Remove a and b from abc to get c. We must delete characters to make both strings anagrams, so we print on a new line.


public class Solution {
public static int numberNeeded(String first, String second) {

    StringBuilder firstBuilder = new StringBuilder(first);
    StringBuilder secondBuilder = new StringBuilder(second);
    int numberNeeded = firstBuilder.length() + secondBuilder.length();

    for (int i=0; i<first.length(); i++) {

        char currentChar = first.charAt(i);

        for (int j=0; j<secondBuilder.length(); j++) {
            char charToCompare = secondBuilder.charAt(j);

            if (charToCompare == currentChar) {
                numberNeeded -= 2;

    return numberNeeded;


public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String firstArray = in.next();
    String secondArray = in.next();
    System.out.println(numberNeeded(firstArray, secondArray));

Can I please get feedback on my code and also on my solution approach? Also, can someone guide me about the time and space complexity of this solution? How do we calculate it and how can I make it better?

Thanks in advance

  • \$\begingroup\$ I get the feeling that this should be somehow accomplishable by an algorithm based on the Levenshtein-Distance computation... \$\endgroup\$
    – Vogel612
    Commented Aug 1, 2017 at 13:17
  • \$\begingroup\$ @Vogel612 i'll have a look at this, I didn't know about this algorithm \$\endgroup\$
    – a-ina
    Commented Aug 1, 2017 at 13:20
  • \$\begingroup\$ @Vogel612 Levenshtein distance is a definite over-engineering here. \$\endgroup\$
    – vnp
    Commented Aug 1, 2017 at 18:00
  • \$\begingroup\$ user vnp below is right. build a map for each string, and then sum up all the differences. 0(n+m) \$\endgroup\$
    – Angela P
    Commented Aug 1, 2017 at 19:56
  • \$\begingroup\$ The variable firstBuilder is redundant, because apart from firstBuilder.length(), its contents are never read, and the length can also be queried from the String first. \$\endgroup\$
    – Stingy
    Commented Aug 1, 2017 at 20:21

1 Answer 1


The time complexity is \$O(NM)\$ where \$N,M\$ are lengths of the strings. Surely is is too much. The solution can be reached in \$O(N+M)\$. In pseudocode:

    build a character histogram from the first string
    build a character histogram from the second string
    for each c in alphabet,
        result += max(hist1[c], hist2[c]) - min(hist1[c], hist2[c])
  • \$\begingroup\$ I like your approach. Since this was the second challenge in the hackerrank, therefore, I thought we were supposed to come up with a solution without using any other data structure like hash maps, vectors, etc. \$\endgroup\$
    – a-ina
    Commented Aug 2, 2017 at 4:56
  • \$\begingroup\$ @a-ina It only requires an array (well, two of them). But don't be afraid to use ADTs as you see fit. Hackerrank doesn't really care, as long as the solution passes. \$\endgroup\$
    – vnp
    Commented Aug 2, 2017 at 5:09
  • \$\begingroup\$ @vpn thankyou. I'll keep this in mind \$\endgroup\$
    – a-ina
    Commented Aug 2, 2017 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.