211. String permutation "Lintcode by Java"

Source: Internet
Author: User

Description

Given-strings, write a method to decide if one was a permutation of the other.

Example

abcdis a permutation bcad of, but are not abbe a permutation ofabe

Problem solving: A similar problem has been encountered, the simpler way is to convert the string into a character array, and then sort, compare the number of each bit is equal. This is less efficient and the code is as follows:

1  Public classSolution {2     /**3      * @parama:a String4      * @paramb:a String5      * @return: A Boolean6      */7      Public Booleanpermutation (string A, String B) {8         //Write your code here9         if(A.length ()! =b.length ())Ten             return false; One         Char[]a =A.tochararray (); A         Char[]B =B.tochararray (); - Arrays.sort (a); - Arrays.sort (b); the          for(inti = 0; i < a.length; i++){ -             if(A[i]! =B[i]) -                return false; -         } +         return true; -     } +}

It can also be done through a hash table: if the number of each element is equal, and the total number is the same, the string is exactly equal, refer to: 51212982.

211. String permutation "Lintcode by Java"

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.