标签:
过年弄得也没写代码了,最近开始继续奋斗
一开始没看懂题目,用了笨方法,就是把每个字母出现的次数记录下来,得了80分,错了两组数据,因为不是VIP暂时看不到数据,想了想也不知道错在哪
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str1 = input.next();
String str2 = input.next();
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1.length()!=str2.length()){
System.out.println("N");
System.exit(0);
}
input(str1,str2);
}
/**
*接收两个串,然后比较
*/
private static void input(String str1,String str2){
int[] arr1 = new int[str1.length()];
int[] arr2 = new int[str2.length()];
for (int i = 0; i < str1.length(); i++) {
arr1[i] = judge(str1.charAt(i),str1);
}
for (int i = 0; i < str2.length(); i++) {
arr2[i] = judge(str2.charAt(i),str2);
}
for (int i = 1; i < str1.length(); i++) {
if (arr1[0] !=arr1[i]){
System.out.println("N");
return;
}
}
for (int i = 1; i < str2.length(); i++) {
if (arr2[0] !=arr2[i]){
System.out.println("N");
return;
}
}
System.out.println("Y");
}
/*
计算字母在一个字符串中出现的次数
*/
private static int judge(char a,String str){
int start = 0,count = 0;
while ((start = str.indexOf(a, start)) >= 0){
start++;
count++;
}
return count;
}
}
,先排序,然后直接比较排序后的数组是否相等即可
private static void judge1(String str1,String str2){
char[] a = str1.toCharArray();
char[] b = str2.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
if (Arrays.equals(a,b)){
System.out.println("Y");
}else {
System.out.println("N");
}
}
标签:
原文地址:http://blog.csdn.net/u012706811/article/details/50662563