boolean isPermutation(String str1, String str2) {
    if (str1 == null || str2 == null) {
        return false;
    }
 
    if (str1.length() != str2.length()) {
        return false;
    }
 
    Map<Character, Integer> letterCounts = new HashMap<Character, Integer>();
 
    for (char c : str1.toCharArray()) {
        Integer currCount = letterCounts.get(c);
 
        if (currCount == null) {
            letterCounts.put(c, 1);
        } else {
            letterCounts.put(c, ++currCount);
        }
    }
 
    for (char c : str2.toCharArray()) {
        Integer str1CharCount = letterCounts.get(c);
 
        //The current char does not exist or exists less times in the first string so they are not permutations
        if (str1CharCount == null || str1CharCount == 0) {
            return false;
        }
 
        //Update the remaining count of the current character
        letterCounts.put(c, str1CharCount - 1);
    }
 
    return true;
}
void removeDuplicates(Node head) {
    if (head == null) {
        return;
    }
       
    Set<Integer> values = new HashSet<Integer>();
    values.add(head.getValue());
    
    while (head != null && head.getNext() != null) {
        Node curr = head.getNext();
        Integer currValue = curr.getValue();
 
        if (values.contains(currValue)) {
            removeNode(head, curr);
        } else {
            values.add(currValue);
        }
 
        head = head.getNext();
    }
}
 
private void removeNode(Node previous, Node remove) {
    if (previous == null || remove == null) {
        return;
    }
 
    Node nextNode = remove.getNext();
    previous.setNext(nextNode);
}

deck

By Reid Harrison

deck

  • 531