package com.sunny.grokkingalgorithms.refresh.ctci.arraysandstrings;

import java.util.HashSet;
import java.util.Set;

public class UniqueCharactersInString {

    /*
    Determine if all characters in a string are unique.
     */

    /**
     *
     * @param input
     * @return
     */
    public static boolean areCharactersUnique(String input){
        //Uses Set
        boolean unique = true;
        Set<Character> set = new HashSet<>();
        for(int i = 0 ; i < input.length() ; i++){
            final char o = input.charAt(i);
            if(set.contains(o)){
                unique = false;
                break;
            }
            set.add(o);
        }
        return unique;
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        String input1 = "abcdef";
        String input2 = "zxcvbjhglzlo";
        System.out.println(areCharactersUnique(input1));
        System.out.println(areCharactersUnique(input2));
    }
}

@@ -700,16 +700,519 @@
 %0A    %7D%0A%0A
+%0A    public static boolean areCharactersUniqueWithoutUsingExtraSpace(String input)%7B%0A        boolean unique = true;%0A        for(int i = 0 ; i %3C input.length() ; i++)%7B%0A            char cur = input.charAt(i);%0A            for(int j = i+1 ; j %3C input.length() ; j++)%7B%0A                if(cur == input.charAt(j))%7B%0A                    unique = false;%0A                    break;%0A                %7D%0A            %7D%0A            if(!unique)%7B%0A                break;%0A            %7D%0A        %7D%0A        return unique;%0A    %7D%0A
     /**%0A
@@ -1474,20 +1474,178 @@
 e(input2));%0A
+        System.out.println(areCharactersUniqueWithoutUsingExtraSpace(input1));%0A        System.out.println(areCharactersUniqueWithoutUsingExtraSpace(input2));%0A
     %7D%0A%7D%0A

Check if string is composed of unique characters without using extra space. O(N.pow(2))