@java 蓝 桥 杯 B group exercises algorithm training (241) ALGO_091 Anagrams problem

Problem Description

Anagrams refer to two words with the following characteristics: In these two words, each English letter (not case sensitive) appears the same number of times. For example, "Unclear" and "Nuclear", "Rimon" and "MinOR" are Anagrams. Write a program, enter two words, and then judge whether the two words are Anagrams. Each word does not exceed 80 characters in length, and is not case sensitive.
  Input format: The input has two lines, which are two words.
  Output format: The output has only one letter Y or N, which means Yes and No respectively.
  Sample input and output Sample
input
Unclear
Nuclear
sample output
Y

Code:

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		String s1=s.next();
		String s2=s.next();
		if(s1.length()!=s2.length()){
			System.out.println("N");
			return;
		}
		s1=s1.toLowerCase();
		s2=s2.toLowerCase();
		int[] a=new int[27];
		int[] b=new int[27];
		for(int i=0;i<s1.length();i++) {
			int n=s1.charAt(i)-'a';
			int m=s2.charAt(i)-'a';
			a[n]++;
			b[m]++;
		}
		for(int i=0;i<27;i++) {
			if(a[i]!=b[i]) {
				System.out.println("N");
				return;
			}
		}
		System.out.println("Y");
	}
}
Now
Published 41 original articles · praised 1 · visits 1293

Guess you like

Origin blog.csdn.net/DAurora/article/details/105400289