Thursday, March 13, 2014

Algorithm Questions and their Java solution

1 ) Finding the longest palindromic substring
Java Solution
Time O(n^2) Space O(1)

public class Palindrom {

       public static void main(String s[]) {
              Palindrom p = new Palindrom();
              String palendrom = p.longestPalindrome("dnoraaroaca");
              System.out.println(palendrom);
       }

       public String longestPalindrome(String s) {
              if (s.isEmpty()) {
                     return null;
              }
              if (s.length() == 1) {
                     return s;
              }
              String longest = s.substring(0, 1);
              for (int i = 0; i < s.length(); i++) {
                     // test [i]
                     String tmp = helper(s, i, i);
                     if (tmp.length() > longest.length()) {
                           longest = tmp;
                     }
                     // test s[i,i+1]
                     tmp = helper(s, i, i + 1);
                     if (tmp.length() > longest.length()) {
                           longest = tmp;
                     }
              }
              return longest;
       }

       // [begin, end]
       public String helper(String s, int begin, int end) {
              while (begin >= 0 && end <= s.length() - 1
                           && s.charAt(begin) == s.charAt(end)) {
                     begin--;
                     end++;
              }
              return s.substring(begin + 1, end);
       }
}




2) Remove Duplicates from Sorted Array (Java)


Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
Java Solution

public int removeDuplicates(int[] A) {
              if (A.length < 2)
                     return A.length;

              int j = 0;
              int i = 1;

              while (i < A.length) {
                     if (A[i] == A[j]) {
                           i++;
                     } else {
                           A[++j] = A[i++];
                     }
              }

              return j + 1;

       }



(b) What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
Java Solution
public int removeDuplicates(int[] A) {
              if (A.length <= 2)
                     return A.length;

              int prev = 1; // point to previous
              int curr = 2; // point to current

              while (curr < A.length) {
                     if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {
                           curr++;
                     } else {
                           prev++;
                           A[prev] = A[curr];
                           curr++;
                     }
              }

              return prev + 1;
       }


3) Permutations (Java)

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
Java solution using Recursion. Swap each element with each element after it.


       public List<List<Integer>> generatePermutation(List<Integer> original) {
              if (original.size() == 0) {
                     List<List<Integer>> result = new ArrayList<List<Integer>>();
                     result.add(new ArrayList<Integer>());
                     return result;
              }
              Integer firstElement = original.remove(0);
              List<List<Integer>> returnValue = new ArrayList<List<Integer>>();
              List<List<Integer>> permutations = generatePermutation(original);
              for (List<Integer> smallerPermutated : permutations) {
                     for (int index = 0; index <= smallerPermutated.size(); index++) {
                           List<Integer> temp = new ArrayList<Integer>(smallerPermutated);
                           temp.add(index, firstElement);
                           returnValue.add(temp);
                     }
              }
              return returnValue;

       }
 all possible permutations in case of duplicate allowJava solution using Recursion. Swap each element with each element after it.

       public void Permute(char str[], int start, int end) {
              if (start >= end) {
                     System.out.println(str);
                     return;
              }
              for (int i = start; i <= end; i++) {
                     if (!duplicate(str, start, i)) {
                           swap(str, start, i);
                           Permute(str, start + 1, end);
                           swap(str, start, i);
                     }
              }

       }


       private boolean duplicate(char str[], int start, int end) {
              if (start == end)
                     return false;
              else
                     for (; start < end; start++)
                           if (str[start] == str[end])
                                  return true;
              return false;

       }

       private void swap(char str[], int m, int n) {
              char temp = str[m];
              str[m] = str[n];
              str[n] = temp;

       }





3) Permutations (Java)


No comments:

Post a Comment