标签:
Write a method to decide if two strings are anagrams or not
解法:
1 排序比较 (本次解法)
2 判断每个字符出现的个数(直接)
import java.util.Arrays; public class Anagrams { public static boolean isAnagrams(String a,String b) { char[] array_a=a.toCharArray(); char[] array_b=b.toCharArray(); Arrays.sort(array_a); Arrays.sort(array_b); if(array_a.length!=array_b.length) { return false; } for (int i = 0; i < array_a.length; i++) { if(array_a[i]!=array_b[i]) return false; } return true; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(isAnagrams("aac", "aca")); System.out.println(isAnagrams("aacd", "aca")); System.out.println(isAnagrams("a", "a")); System.out.println(isAnagrams("", "")); System.out.println(isAnagrams("aaadd", "ddaaa")); } }
标签:
原文地址:http://www.cnblogs.com/AlexWei/p/4943684.html