Determine if a string has all Unique Characters | Java program to illustrate String with unique characters without using any data structure ; Assuming string can have characters a - z this has 32 bits set to 0 ; if that bit is already set in checker , return false ; otherwise update and continue by setting that bit in the checker ; no duplicates encountered , return true ; Driver Code
import java . util . * ; class GfG { boolean uniqueCharacters ( String str ) { int checker = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int bitAtIndex = str . charAt ( i ) - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) return false ; checker = checker | ( 1 << bitAtIndex ) ; } return true ; } public static void main ( String args [ ] ) { GfG obj = new GfG ( ) ; String input = " geekforgeeks " ; if ( obj . uniqueCharacters ( input ) ) System . out . println ( " The ▁ String ▁ " + input + " ▁ has ▁ all ▁ unique ▁ characters " ) ; else System . out . println ( " The ▁ String ▁ " + input + " ▁ has ▁ duplicate ▁ characters " ) ; } }