Quickly compares characters in two strings exactly the same: that is, brother string comparisons

Source: Internet
Author: User

Just on the Internet, see this problem in many forums to get a lot of discussion, so try to practice a bit.

"Problem description"

For two strings, determines whether the included characters are exactly the same. For example, "Sabac" and "Basca" contain exactly the same characters, and the same number of characters is the same, but they can be different in order.

"Problem Analysis"

1. Determine if the length of the two strings is the same

2. Determine whether the characters in a string of the same length and the same number of characters are the same.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

3. When judging characters, the string is cut into an array of strings, then the character data is sorted, and then the corresponding characters of two strings are compared.

"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;  
}

Returns True the same, otherwise returns false;

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.