Quickly compare two strings with identical characters (brother string comparison)

Source: Internet
Author: User

I had a big discussion on this issue in many forums just now, so I tried to practice it.

[Problem description]

Determine whether the two strings contain identical characters. For example, "sabac" and "basca" contain exactly the same characters, and the numbers of the same characters must be the same, but their order can be different.

[Problem Analysis]

1. first determine whether the two strings are of the same length

2. Check whether the numbers of characters in a string of the same length are the same.

3. When judging characters, first cut the string into a string array, then sort the character data, and then compare the characters corresponding to the two strings.

[Code implementation]
Public boolean isIncludeSameChar (String str1, String str2) {boolean flag = true; if (str1.length ()! = Str2.length () {flag = false;} else {char [] str1Arr = str1.toCharArray (); Arrays. sort (str1Arr); char [] str2Arr = str2.toCharArray (); Arrays. sort (str2Arr); for (int I = 0; I <str2Arr. length; I ++) {if (str2Arr [I] = str1Arr [I]) {continue;} else {flag = false ;}} return flag ;}

True is returned, otherwise false is returned;



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.