Game of Thrones - I

  • + 0 comments

    My solution is similar, but I added a condition at the beginning of the loop to make it quit as soon as we know there is no chance for it to be a palindrome :)

    HashSet<Character> hashSet = new HashSet<Character>();
    for(int i=0;i<word.length();i++){
        if(hashSet.size() > (word.length()-i) + 1) return false;
        char character = word.charAt(i);
        if(hashSet.contains(character)){
            hashSet.remove(character);
        }else{
            hashSet.add(character);
        }
    }
    return hashSet.size() == word.length()%2;