Cc150-array and String 1.1

Source: Internet
Author: User

Promble:

Implement an algorithm to determine if a string have all unique characters. What if do cannot use addtional data structure?

My Solution:

1.ckeck wether the length of STR is really equal to 0 or more than, if yes, it would return false

2. ASCII has a character

3. Use ToArray function chage string as char array

4. Sort Char Array

5. Compare between-character, get duplicate or not

Code

public static Boolean different (String str) {
if (Str.length () ==0| | str==null| | Str.length () >256) return false;
char[] strlist = Str.tochararray ();
Arrays.sort (strlist);
for (int i= 1; i<strlist.length; i++) {
if (Strlist[i]==strlist[i-1])
return true;
}
return false;
}

The given solution:

1. Build a Boolean Array and record the character appear on the string according the ASCII value

2. If corresponding ASCII value is exist and then return false.

Code

public static Boolean isUniqueChars2 (String str) {

if (Str.length () ==0| | Str.length () >256) return false;

boolean[] Char_set = new boolean[256];

for (int i =0; i < str.length (); i++) {

int value = Str.charat (i)

if (Char_set[value]) {

return false;

}

Char_set[value]=true;

}

return true;

}

Cc150-array and String 1.1

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.