Implement an algorithm that determines whether all characters of a string are all different. Here we require that additional storage structures not be allowed

Source: Internet
Author: User

* * Implement an algorithm that determines whether all characters of a string are all different. Here we require that additional storage structures not be allowed to be used.
Given a string inistring, return a bool value, true to mean that all characters are different, and false to represent the same characters. Guarantees that the characters in the string are ASCII characters. The length of the string is less than or equal to 3000.
Test Sample:
"Aeiou"
Return: True
"BarackObama"
Back: false**

Package question1_1;
            public class Question {public static Boolean isuniquechars (String str) {if (Str.length () > 128) {
        return false;
        int checker = 0;
            for (int i = 0; i < str.length (); i++) {int val = Str.charat (i)-' a ';
            if ((Checker & (1 << val)) > 0) return false;
        Checker |= (1 << val);
    return true;
        public static Boolean isUniqueChars2 (String str) {if (Str.length () > 128) {return false;
        } boolean[] Char_set = new boolean[128];
            for (int i = 0; i < str.length (); i++) {int val = Str.charat (i);
            if (Char_set[val]) return false;
        Char_set[val] = true;
    return true;
        public static void Main (string[] args) {string[] words = {"ABCDE", "Hello", "apple", "Kite", "Padle"}; for (String word:words) {System.out.println (Word + ":" + isuniquechars (Word) + "" + isUniqueChars2 (word));
 }
    }

}

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.