Make it Anagram

来源:互联网 发布:php后端开发教程 编辑:程序博客网 时间:2024/06/05 10:13

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:

cdeabc

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);         }}


0 0
原创粉丝点击