158. Valid Anagram "Lintcode by Java"

Source: Internet
Author: User
Tags first string lintcode

Description

Write a method to anagram(s,t) decide if strings is anagrams or not.

Clarification

What is anagram?

    • Strings is anagram if they can is the same after change the order of characters.
Example

Given s = "abcd" , t = "dcab" , return true .
Given s = "ab" , t = "ab" , return true .
Given s = "ab" , t = "ac" , return false .

Challenge

O (n) time, O (1) Extra space

Problem solving: The topic given two strings, judging whether the two strings in addition to the character sequence is different, is equal. The easiest thing to think about is to sort the characters and determine if the bitwise characters are equal. In Java, however, to sort the characters in a string and only convert it to a character array, the challenge condition is not met. But with other methods (such as a hash table), the code can only handle characters in letters or ASCII, which is detrimental to generality. Paste the code for the sorting method:

1  Public classSolution {2     /**3      * @params:the First String4      * @paramt:the Second String5      * @return: TRUE or False6      */7      Public BooleanAnagram (string s, String t) {8         //Write your code here9         Char[]arr_s =S.tochararray ();Ten         Char[]arr_t =T.tochararray (); One Arrays.sort (arr_s); A Arrays.sort (arr_t); -         returnstring.valueof (arr_s). Equals (String.valueof (arr_t)); -     } the}

158. Valid Anagram "Lintcode by Java"

Related Article

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.