Make it Anagram

Problem Statement

Chinese Version
Russian Version

Alice recently started learning about cryptography and found that anagrams are very useful. Two strings are anagrams of each other if they have same character set. For example strings"bacdc" and "dcbac" are anagrams, while strings "bacdc" and "dcbad" are not.

Alice decides on an encryption scheme involving 2 large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. She need your help in finding out this number.

Given two strings (they can be of same or different length) help her in finding out the minimum number of character deletions required to make two strings anagrams. Any characters can be deleted from any of the strings.

Input Format 
Two lines each containing a string.

Constraints 
1 <= Length of A,B <= 10000 
A and B will only consist of lowercase latin letter.

Output Format 
A single integer which is the number of character deletions.

Sample Input #00:

cde
abc

Sample Output #00:

4

Explanation #00: 
We need to delete 4 characters to make both strings anagram i.e. 'd' and 'e' from first string and 'b' and 'a' from second string.

//理解錯了題目意思,是可以通過任意變換來成爲<span style="color: rgb(57, 66, 78); font-family: 'Whitney SSm A', 'Whitney SSm B', verdana, 'Lucida Grande', sans-serif; font-size: 18px; line-height: 27px;">anagrams</span>
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
   public static void op(String s1,String s2){
    int n1=s1.length();
    int n2=s2.length();
    int[] array1=new int[26];
    int[] array2=new int[26];
    for(int i=0;i<n1;i++){
        char c=s1.charAt(i);
        array1[c-'a']++;
    }
    for(int i=0;i<n2;i++){
        char c=s2.charAt(i);
        array2[c-'a']++;
    }
    int sum=0;
    for(int i=0;i<26;i++){
        sum+=Math.abs(array1[i]-array2[i]);
    }
    System.out.println(sum);
}
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
         Scanner cin=new Scanner(System.in);
        
         String a=cin.next();
         String b=cin.next();
        op(a,b);
     
    }
}


發佈了87 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章