algorithm

#hackerrank#Making Anagrams#algorithm#알고리즘

hoon.lucky7 2020. 6. 30. 22:52

Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.

Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?

Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make  a and b anagrams. Any characters can be deleted from either of the strings.

For example, if a = cde and b = dcf, we can delete  from string a and f from string b so that both remaining strings are cd and  dc which are anagrams.

Function Description

Complete the makeAnagram function in the editor below. It must return an integer representing the minimum total characters that must be deleted to make the strings anagrams.

makeAnagram has the following parameter(s):

  • a: a string
  • b: a string

Input Format

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

Constraints

  • 1 <= |a|, |b| <= 10^4
  • The strings  a and  b consist of lowercase English alphabetic letters ascii[a-z].

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

cde

abc

Sample Output

4

Explanation

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

  1. Remove d and e from cde to get c.
  2. Remove a and b from abc to get c.

We must delete 4 characters to make both strings anagrams, so we print  4 on a new line.

 

problem link : 

https://www.hackerrank.com/challenges/ctci-making-anagrams/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings

 

Strings: Making Anagrams | HackerRank

How many characters should one delete to make two given strings anagrams of each other?

www.hackerrank.com

* 해결 전략

1) "a"~"z"는 26개임을 생각하자

2) 배열 첨자 0을 "a"로 가정하고 26개 배열을 countA, countB 만들어서 거기에 카운트를 한다

예를 들어 "b"가 나오면 1번째에 카운트 한다.

3) |countA - countB|이 지워야 하는 개수이다.

 

* source

 

static int makeAnagram(String a, String b) {
        int[] countA = new int[30];
        int[] countB = new int[30];
        int count = 0;

        for (int i = 0; i < 30; i++) {
            countA[i] = countB[i] = 0;
        }

        for (int i = 0; i < a.length(); i++) {
            countA[a.charAt(i) - 'a']++;
        }

        for (int i = 0; i < b.length(); i++) {
            countB[b.charAt(i) - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            count += Math.abs(countA[i] - countB[i]);
        }
        return count;
    }