Created
March 17, 2013 19:17
-
-
Save astkaasa/5183140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class UniqueChar { | |
public static void main( String[] args ) { | |
String[] strArray = { "abcdef", "dssfagagalglag" }; | |
for ( String str : strArray ) | |
System.out.println( isUniqueChar1( str ) ); | |
for ( String str : strArray ) | |
System.out.println( isUniqueChar2( str ) ); | |
for ( String str : strArray ) | |
System.out.println( isUniqueChar3( str ) ); | |
} | |
//bitmap method | |
public static boolean isUniqueChar1( String str ) { | |
boolean [] set = new boolean[256]; | |
for ( int i = 0; i < 256; i++ ) | |
set[i] = false; | |
int len = str.length(); | |
for ( int j = 0; j < len; j++ ) { | |
int val = str.charAt( j ); | |
if ( set[val] == false ) | |
set[val] = true; | |
else return false; | |
} | |
return true; | |
} | |
//hash table method | |
public static boolean isUniqueChar2( String str ) { | |
Map<Integer, Character> charMap = new HashMap<Integer, Character>(); | |
for ( int i = 0; i < str.length(); i++ ) { | |
char val = str.charAt( i ); | |
int key = val; | |
if ( ( charMap.containsKey( key ) ) == true ) | |
return false; | |
else charMap.put( key, val ); | |
} | |
return true; | |
} | |
//quicksort | |
public static boolean isUniqueChar3( String str ) { | |
char [] data = str.toCharArray(); | |
Arrays.sort( data ); | |
for ( int i = 0, arrlen = data.length; i < arrlen - 1; i++ ) { | |
if ( data[i] == data[i + 1] ) | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment