Topic:
Given, Strings s and T, write a function to determine if it is a anagram of s.
For example,
s = "Anagram", t = "Nagaram", return true.
s = "Rat", t = "Car", return false.
Note:
Assume the string contains only lowercase alphabets.
Exercises
A hash table is used to record each character and the number of occurrences of one of the strings. Use the character of another string as key to get the value to determine if it exists.
public static Boolean Isanagram (string s, String t) { if (s.length ()!=t.length ()) return false; Char[] Array=s.tochararray (); Hashmap<character, integer> hashmap=new hashmap<> (); for (int i=0;i<array.length;i++) { if (Hashmap.get (Array[i]) ==null) { hashmap.put (array[i], 1); } else {int temp=hashmap.get (array[i]); Hashmap.put (Array[i], temp+1);} } for (int k=0;k<s.length (); k++) { char stemp=t.charat (k); if (Hashmap.get (stemp)!=null) { int value=hashmap.get (stemp); if (value>=1) { hashmap.put (stemp, value-1); } else {return false;} } else {return false;} } return true; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode242_valid Anagram (determines whether two strings are composed of exactly the same characters) Java