/*Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string contains only lowercase alphabets.*/ public static boolean isAnagram(String s, String t) { int[] ch1 = new int[26]; char[] cha1 = s.toCharArray(); char[] cha2 = t.toCharArray(); if (cha1.length != cha2.length) return false; for (int i = 0; i < cha1.length; i++) { int temp = cha1[i] - 97; ch1[temp]++; } for (int i = 0; i < cha2.length; i++) { int temp = cha2[i] - 97; ch1[temp]--; } for (int i : ch1) { if (i != 0) return false; } return true; } }
时间: 2024-10-08 02:35:18