package chapter_1;

import java.io.FileNotFoundException;
import java.util.Arrays;

import util.InputUtil;

/**
 * Question 1.2: Given two strings, write a method to determine whether one is a
 * permutation of the other.
 * 
 * @author Sudharsanan Muralidharan
 */
public class CheckPermutation {

  /**
   * Check if first string is a permutation of the second: Sort the strings and
   * compare each character: Complexity: O(nlogn)
   * 
   * @param first
   * @param second
   * @return isPermuatation
   */
  private boolean isPermutationSorted(String first, String second) {

    if (first.length() != second.length()) {
      return false;
    } else {
      char[] firstChars = first.toCharArray();
      char[] secondChars = second.toCharArray();

      Arrays.sort(firstChars);
      Arrays.sort(secondChars);

      for (int i = 0; i < firstChars.length; i++) {
        if (firstChars[i] != secondChars[i]) {
          return false;
        }
      }
    }

    return true;
  }

  /**
   * Check if first string is a permutation of the second: Use an Array[128] to
   * maintain a counter for each character, decrement while checking the second
   * String. If any value < 0 -> return false: Complexity: O(n), Space: O(1)
   * 
   * @param first
   * @param second
   * @return isPermutation
   */
  private boolean isPermutationArray(String first, String second) {
    int[] counter = new int[128];

    if (first.length() != second.length()) {
      return false;
    } else {
      int len = first.length();
      for (int i = 0; i < len; i++) {
        counter[first.charAt(i)]++;
      }

      for (int i = 0; i < len; i++) {
        counter[second.charAt(i)]--;
      }

      for (int i = 0; i < counter.length; i++) {
        if (counter[i] < 0) {
          return false;
        }
      }
    }

    return true;
  }

  public static void main(String[] args) throws FileNotFoundException {

    CheckPermutation checkPermutation = new CheckPermutation();
    String[] input = InputUtil.readContents(1, "check_permutation");
    String first, second;

    for (String line : input) {
      first = line.split(":")[0].trim();
      second = line.split(":")[1].trim();

      if (first != null && second != null && !first.isEmpty() && !second.isEmpty()) {
        System.out.println("Input: " + line);
        System.out.println("Permutation check using sorted: " + checkPermutation.isPermutationSorted(first, second));
        System.out.println("Permutation check using array: " + checkPermutation.isPermutationArray(first, second));
        System.out.println();
      }
    }

  }
}

@@ -824,65 +824,28 @@
     
-for (int i = 0; i %3C firstChars.length; i++) %7B%0A        if 
+return Arrays.equals
 (fir
@@ -855,14 +855,9 @@
 hars
-%5Bi%5D !=
+,
  sec
@@ -868,80 +868,16 @@
 hars
-%5Bi%5D) %7B%0A          return false;%0A        %7D%0A      %7D%0A    %7D%0A%0A    return true;
+);%0A    %7D
 %0A  %7D
@@ -1727,16 +1727,769 @@
 e;%0A  %7D%0A%0A
+  /**%0A   * Check if first string is a permutation of the second: Use 2 bit vectors of%0A   * length 32 to maintain a counter for each character. XOR of the bit vectors%0A   * value != 0 -%3E return false:%0A   * %0A   * Complexity: O(n), Space: O(1)%0A   * %0A   * @param first%0A   * @param second%0A   * @return isPermutation%0A   */%0A  private boolean isPermutationBitWise(String first, String second) %7B%0A    int checker = 0, checker2 = 0;%0A%0A    if (first.length() != second.length()) %7B%0A      return false;%0A    %7D%0A%0A    int len = first.length();%0A    for (int i = 0; i %3C len; i++) %7B%0A      checker %7C= (1 %3C%3C (first.charAt(i) - 'a'));%0A    %7D%0A%0A    for (int i = 0; i %3C len; i++) %7B%0A      checker2 %7C= (1 %3C%3C (second.charAt(i) - 'a'));%0A    %7D%0A%0A    return (checker %5E checker2) == 0;%0A  %7D%0A%0A
   public
@@ -3187,32 +3187,152 @@
 irst, second));%0A
+        System.out.println(%22Permutation check using bitwise: %22 + checkPermutation.isPermutationBitWise(first, second));%0A
         System.o

Add method to use bit vector to check permutation