# [Leetcode]: 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.

Analysis:

Criteria for distinguishing "anagram":

1. Two characters are exactly equal and two numbers are completely empty: true

2. Two characters unequal length: false

3. The number of occurrences of the same character is not equal: false

The code is as follows:

` Public classSolution { Public Static inthascharacter (String Basic, String Target) { for(inti = 0;i<basic.length (); i++){            if(Basic.substring (i, i+1) = =Target) {                returni; }        }                return-1; }         Public Static BooleanIsanagram (string s, String t) {if(S.length ()! =t.length ()) {            return false; }                if(s = =t) {            return true; }                 for(intI=0;i<t.length (); i++){            intIflag = Hascharacter (s,t.substring (i, i+1)); if(Iflag = =-1){                return false; }            Else{s= S.substring (iflag,iflag+1); }        }        return true; }}`

Master's idea: convert two characters to a string and then compare them after sorting

`     Public Static BooleanIsanagram (string s, String t) {if(s = = T | | (S.equals ("") && t.equals (""))){            return true; }                if(S.length ()! =t.length ()) {            return false; }                Char[] CharS =S.tochararray (); Char[] CharT =T.tochararray ();        Arrays.sort (CharS);                Arrays.sort (CharT); returnchars.equals (CharT); }`

[Leetcode]: 242:valid anagram

Related Keywords:

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.