242. Valid Anagram

Source: Internet
Author: User

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.

Follow up:
What if the inputs contain Unicode characters? How would adapt your solution to such case?

Links: http://leetcode.com/problems/valid-anagram/

Exercises

Verify the anagram. There are many solutions to this problem.

The simplest is to sort the array after the comparison.

Time Complexity-o (NLOGN), Space complexity-o (n)

 Public classSolution { Public BooleanIsanagram (string s, String t) {if(s = =NULL|| t = =NULL|| S.length ()! =t.length ())return false; if(s.length () = = 0)            return true; Char[] SArr =S.tochararray (); Char[] TArr =T.tochararray ();        Arrays.sort (SARR);                Arrays.sort (TARR); returnarrays.equals (SARR, TARR); }}

We can also use HashMap, traverse s to save the elements in the map, and then traverse t to check if there are exactly the same elements and quantities as s.

Time Complexity-o (n), Space complexity-o (1). But the actual speed of operation is rather slow.

 Public classSolution { Public BooleanIsanagram (string s, String t) {if(s = =NULL|| t = =NULL|| S.length ()! =t.length ())return false; if(s.length () = = 0)            return true; Map<character, integer> map =NewHashmap<>();  for(inti = 0; I < s.length (); i++) {            Charc =S.charat (i); if(Map.containskey (c)) Map.put (c, Map.get (c)-W); ElseMap.put (c,1); }                 for(inti = 0; I < t.length (); i++) {            Charc =T.charat (i); if(!Map.containskey (c))return false; Else {                if(Map.get (c) = = 0)                    return false; Map.put (c, Map.get (c)-1); }        }                return true; }}

can also use bitmap, because the title description only contains lowercase letters, so we can build a length of 26 bitmap, while traversing s and T, increment bitmap[char S-' a '], decrement bitmap[char t-' a '], Finally check if each bit is 0, yes returns true, otherwise false is returned.

 Public classSolution { Public BooleanIsanagram (string s, String t) {if(s = =NULL|| t = =NULL|| S.length ()! =t.length ())return false; if(s.length () = = 0)            return true; int[] BitMap =New int[26];  for(inti = 0; I < s.length (); i++) {Bitmap[s.charat (i)-' A ']++; Bitmap[t.charat (i)-' A ']--; }                 for(intI:bitmap)if(I! = 0)                return false; return true; }}

Reference:

Https://leetcode.com/discuss/57903/java-solution-using-sort

Https://leetcode.com/discuss/49399/accepted-java-o-n-solution-in-5-lines

Https://leetcode.com/discuss/61456/my-java-solution-8ms

Https://leetcode.com/discuss/49795/share-my-java-solution

Https://leetcode.com/discuss/50631/jave-simple-and-efficient-solution

242. Valid Anagram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.