哎匹克网上测试

1.哎,找工不容易,开始报我的历史。。。
第一篇是哎匹克的,这公司我网投的,第二天就联系我网上做题,效率挺高的。
网上做了大概4个小时,然后过了几天说我没过,拉到,但一年以后我又报了这公司,HR看见我之前已经做过OA了就直接叫了上门面了。感觉这OA像是一年前种种子,然后会自己开花结果。下面是些总结。感觉他们喜欢permutation这样的题目。

(1) adjustedAverage
(2)三角打印
和原题不同,是最上面一行最长,往下逐次少一个元素。

public static int[][] triangleNum (int[] nums) {
    if (nums == null || nums.length == 0) {
        throw new IllegalArgumentException();
    }
    int len = nums.length; 
    int[][] result = new int[len][];
    int index = 0;
    result[index++] = nums;
    int[] prev = nums;
    while (prev.length > 1) {
        int[] curr = new int[prev.length-1];
        for (int i = 0; i < curr.length; i++) {
            curr[i] = prev[i] + prev[i+1];
        }
        prev = curr;
        result[index++] = curr; 
    } 
    return result;     
}

(3)swapping string
(4) snake sequence
写完了发现BFS 四个扩展时的范围判断漏掉了。还掉了
board[x][y]=‘Z’;

1)calendar, 给一个日期,mm/dd/yyyy, 输出下一个leapday的日期。需
要 validate 输入.
2)统计一个string 里面的 character 数量
3)snake,给一个矩阵,求最长的snake数组,输出所有符合最长snake的数组。注意会有好几条符合要求的数组,都要输出.
4)finger typing

A number can be broken into different sub-sequence parts. Suppose a number 3245 can be
broken into parts like 3 2 4 5 32 24 45 324 245. And this number is a colorful number, since
product of every digit of a sub-sequence are different. That is,3 2 4 5 (32)=6 (24)=8
(45)=20 (324)= 24 (245)= 40. But 326 is not a colorful number as it generates 3 2 6
(3
2)=6 (2*6)=12. You have to write a function that tells if the given number is a colorful
number or not.

class Ideone
{
    public static boolean isColorful (int num) {
    if (num < 0) {
        throw new IllegalArgumentException();
    }
    Set<Integer> set = new HashSet<>();
    String numStr = Integer.toString(num);
    for (int i = 1; i < numStr.length(); i++) {
        for (int j = 0; j <= numStr.length() - i; j++) {
            if (isDuplicate(numStr.substring(j, j + i), set)) {
                return false;
            }
        }
    }
    return true; 
} 

private static boolean isDuplicate (String s, Set<Integer> set) {
    int product = 1;
    for (int i = 0; i < s.length(); i++) {
        int temp = (int) (s.charAt(i) - '0');
        product *= temp;
    }
    if (set.contains(product)) {
        return true; 
    } else {
        set.add(product);
        return false; 
    }
}
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(isColorful(326));
    }
}

  1. Well-ordered String:
    You know a password is well-ordered string. Well-ordered string means that the order of the
    characters is in an alphabetical increasing order. Like “abEm” is a well-ordered number.
    However, “abmE” is not a well-order number. Given an input# that tells you also how many
    digits are in the password, print all possible passwords.
class Ideone
{
    public static List<String> wellorded (int len) {
    if (len <= 0 || len > 26) { // 注意这里上限也有限制。
        throw new IllegalArgumentException();
    }
    List<String> list = new ArrayList<>();
    helper(0, len, list, "");
    return list;
}

    private static void helper (int start, int left, List<String> list, String curr) {
        if (left == 0) {
            list.add(curr);
            return; 
        }
        for (int i = start; i <= 26 - left; i++) {
            helper(i + 1, left - 1, list, curr + (char) (i + 'a'));
            helper(i + 1, left - 1, list, curr + (char) (i + 'A'));
        }
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> list = wellorded(2);
        for (String s: list) {
            System.out.println(s);
        }
    } 
}
  1. Desirable Number
    A number is called ‘desirable’ if all the digits are strictly ascending eg: 159 as 1<5<9. You know that your rival has a strictly numeric password that is ‘desirable’. Your close ally has given you the number of digits (N) in your rival’s password. WAP th\hjtat takes in ‘N’ as input and prints out all possible ‘desirable’ numbers that can be formed with N digits.
class Ideone
{
    public static List<String> desirableNum (int num) {
        if (num < 1 || num > 10) throw new IllegalArgumentException("Input should between 1 and 10");
        List<String> result = new ArrayList<>();
        findDesirableNum(0, result, "", num);//这题要是0也要考虑在内就必须用string.
        return result;
    }
    private static void findDesirableNum (int start, List<String> result, String temp, int left) {
        if (left == 0) {
            result.add(temp);
            return; 
        }
        for (int i = start; i <= 10 - left; i++) {
            findDesirableNum(i + 1, result, temp + i, left - 1);
                                  //注意在这之前如果对内容有所改变会影响后面所有for 里面的内容。 
        }
        return; 
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> ls = desirableNum(2);
        for (String s: ls) {
            System.out.println(s);
        }
    }
}
  1. Telephone Number
    Print all valid phone numbers of length n subject to following constraints:
    If a number contains a 4, it should start with 4
    No two consecutive digits can be same
    Three digits (e.g. 7,2,9) will be entirely disallowed, take as input
class Ideone
{
    public static List<String> telephoneNum (int len, Integer numOne, Integer numTwo, Integer numThree) {
        if (len < 1) {
            throw new IllegalArgumentException();
        }
        List<Integer> list = new ArrayList<>();
        List<String> result = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        list.remove(numOne);
        list.remove(numTwo);
        list.remove(numThree);
        if (numOne != 4 && numTwo != 4 && numThree != 4) { // handle 4 
            Integer four = new Integer(4);
            helper(list, result, "4", four, len - 1);
            list.remove(four);
        }
        for (Integer i: list) {
            helper(list, result, "" + i, i, len - 1);
        }
        return result; 
    }

    private static void helper (List<Integer> options, List<String> result, String curr, Integer pre, int left) {
        if (left == 0) {
            result.add(curr);
            return; 
        }
        for (Integer i: options) { //记得要避免
            if (i.equals(pre)) {  //Integer 比较一定要用equals
                continue; 
            }
            helper(options, result, curr + i, i, left - 1);
        }
    }  
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> list = telephoneNum(3, 2, 3, 6);
        for (String s: list) {
            System.out.println(s);
        }
    }
}
  1. SMS
    You are given a telephone keyboard
    0-0, 1-1, 2-ABC2, 3-DEF3, 4-GHI4, 5-JKL5, 6-MNO6,7-PQRS7, 8-TUV8, 9-WXYZ9, -space, #-char separater
    if you type “2”, you will get ‘A’, that is “2”-‘A’, “22”-‘B’ ,“222”-‘C’, “2222”-‘D’
    However, the digits can repeated many times
    “22222”-you get ‘A’ again
    You can use “#” to separate characters, for example
    “2#22”, you get “AB”
    However, you may also have consecutive different digits without separator:“23”-‘AD’
    If you type "
    ", it means space…
    You a given a sequence of digits, translate it into a text message
class Ideone
{
    public static String sms (String input) {
        if (input == null) {
            throw new IllegalArgumentException();
        }
        String[] pattern = {"0","1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PORS7", "TUV8", "WXTZ9"};
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            char curr = input.charAt(i);
            if (curr == '#') {
                continue;
            } 
            if (curr == '*') {
                sb.append(" ");
                continue;
            }
            if (curr > '9' || curr < '0') {
                throw new IllegalArgumentException();
            }
            int len = 1;
            while (i + len < input.length() && input.charAt(i + len) == curr) {    
                len++;
            }
            String temp = pattern[curr-'0'];
            char toAdd = temp.charAt((len - 1) % temp.length());
            sb.append(toAdd);
            i += len - 1;  //这里注意要-1因为后面的循环会+1
        }
        return sb.toString();
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(sms("44433333*2#222"));
    }
}
  1. Keypad Permutation
    Phone has letters on the number keys. For example, number 2 has ABC on it, number 3 has DEF, 4 number has GHI,… , and number 9 has WXYZ. Write a program that will print out all of the possible combination of those letters depending on the input.
class Ideone
{
    private static List<String> permutation (String input) {
        if (input == null) throw new IllegalArgumentException("Invalid Input");
        String[] pattern = {"","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"};
        List<String> list = new ArrayList<>();
        list.add("");
        
        for (char digit: input.toCharArray()) {
            int index = digit - '0';
            if (index < 2 || index > 9) throw new IllegalArgumentException("Invalid Input");
            String temp = pattern[digit - '0'];
            List<String> pre = list;
            list = new ArrayList<>();
            for (char option: temp.toCharArray()) {
                for (String s: pre) {
                    s += option;
                    list.add(s);
                }
            }
        }
        return list; 
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(permutation("498237"));
    }
}
public static List<String> keyPad (String input) {
        if (input == null) {
            throw new IllegalArgumentException();    
        }
        StringBuilder sb = new StringBuilder();
        String[] pattern = {"","","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"};
        List<String> result = new ArrayList<>();
        helper(pattern, input, "", 0, result);
        return result; 
    }
    
    private static void helper (String[] pattern, String input, String curr, int index, List<String> result) {
        if (index == input.length()) {    
            result.add(curr);
            return; 
        }
        int n = (int) (input.charAt(index) - '0');
        if (n > 9 || n < 2) {
            throw new IllegalArgumentException();
        }
        String options = pattern[n];
        for (char c: options.toCharArray()) {
            helper(pattern, input, curr + c, index + 1, result);
        }    
    }
  1. The stepping number:
    A number is called as a stepping number if the adjacent digits are having a difference of 1.
    For eg. 8,343,545 are stepping numbers. While 890, 098 are not. The difference between
    a ‘9’ and ‘0’ should not be considered as1. Given start number(s) and an end number (e)
    your function should list out all the stepping numbers in the range including both the
    numbers s & e.
 public static List<String> steppingNum (int start, int end) {
        if (start < 0 || end < start) {
            throw new IllegalArgumentException();
        }
        int sLength = (int) Math.floor(Math.log10(start)) + 1;
        int eLength = (int) Math.floor(Math.log10(end)) + 1;
        List<String> result = new ArrayList<>();
        for (int i = sLength; i <= eLength; i++) {
            for (int j = 1; j < 10; j++) {
                helper(result, "" + j, j, i - 1, start, end);
            }
        }
        return result; 
    }
    
    private static void helper (List<String> result, String curr, int prev, int left, int start, int end) {
        if (left == 0) {
            int toAdd = Integer.parseInt(curr);
            if (toAdd >= start && toAdd <= end) {
                result.add(curr);
            }
            return; 
        }
        if (prev != 0) {
            int thisDigit = prev - 1;
            helper(result, curr + thisDigit, thisDigit, left - 1, start, end);
        }
        if (prev != 9) {
            int thisDigit = prev + 1;
            helper(result, curr + thisDigit, thisDigit, left - 1, start, end);
        }
    }
  1. Two Primes
    Goldbach’s conjecture : Every even integer greater than 2 can be expressed as the sum of
    two primes. Write a function which takes a number as input, verify if is an even number
    greater than 2 and also print at least one pair of prime numbers.
public static void twoPrimes (int num) {
    if (num < 3) {
        throw new IllegalArgumentException();
    }
    for (int i = 1; i <= num / 2; i += 2) {
        if (isPrime(i) && isPrime(num - i)) {
            System.out.println(num + " = " + i + " + " + (num - i));
        }
    }
}

private static boolean isPrime (int num) {
    if (num % 2 == 0) return false;
    for (int i = 3; i <= Math.sqrt(num); i += 2) {
        if (num % i == 0) {
            return false;
        }
    }
    return true; 
}

  1. Finding Words
    Write a program for a word search. If there is an NxN grid with one letter in each cell. Let
    the user enter a word and the letters of the word are said to be found in the grid either the
    letters match vertically, horizontally or diagonally in the grid. If the word is found, print the
    coordinates of the letters as output.
public static void main(String[] args) {
  // TODO Auto-generated method stub
   char[][] board = {
   { 'l', 'a', 'c', 'd' }, 
   { 'e', 'f', 'g', 'h' },
   { 'i', 'j', 'a', 'l' }, 
   { 'm', 'n', 'o', 'p' } 
  };
   System.out.println(findWord(board, "al"));
 }
 
 public static Set<List<String>> findWord(char[][] grid, String word) {
  Set<List<String>> rst = new HashSet<>();
  if (word == null || word.length() == 0)
   return rst;
  if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0)
   return rst;
  
  int[][] direction = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 },{ -1,-1 },{ -1,1 },{ 1,-1 },{ 1,1 } };
  for (int row = 0; row < grid.length; row++) {
   for (int col = 0; col < grid[0].length; col++) {
    if (grid[row][col] == word.charAt(0)) {
     dfs(rst, grid, word, 0, row, col, new ArrayList<String>(), direction);
    }
   }
  }
  return rst;
 }
 
 private static void dfs(Set<List<String>> rst, char[][] grid, String word, int curr, 
   int row, int col, List<String> list, int[][] direction) {
  if (curr == word.length()) {
   rst.add(new ArrayList<>(list));
   return;
  }
  if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) // 记得要查边界
   return;
  if (grid[row][col] != word.charAt(curr))
   return;
  
  grid[row][col] ^= 0;
  list.add("(" + row + "," + col + ")");
  for (int i = 0; i < direction.length; i++) {
   dfs(rst, grid, word, curr + 1, row + direction[i][0], col + direction[i][1], list, direction);
  }
  list.remove(list.size() - 1);
  grid[row][col] ^= 0;
 }
  1. Spiral Matrix
    Given a NXN matrix, starting from the upper right corner of the matrix start printing values in a
    counter-clockwise fashion.
    E.g.: Consider N = 4
    Matrix= {a, b, c, d,
    e, f, g, h,
    i, j, k, l,
    m, n, o, p}
    Your function should output: dcbaeimnoplhgfjk
public static String spiralMatrix (char[][] grid) {
    if (grid == null || grid.length != grid[0].length) {
        throw new IllegalArgumentException();
    }
    int row = grid.length;
    int col = grid[0].length;
    int i = 0;
    int j = col;
    StringBuilder sb = new StringBuilder();
    while (true) {
        //to left
        for (int times = 0; times < col; times++) {
            sb.append(grid[i][--j]);
        }
        if (--row == 0) {
            break;
        }
        //down
        for (int times = 0; times < row; times++) {
            sb.append(grid[++i][j]);
        }
        if (--col == 0) {
            break;
        }
        //right
        for (int times = 0; times < col; times++) {
            sb.append(grid[i][++j]);
        }
        if (--row == 0) {
            break;
        }
        //up
        for (int times = 0; times < row; times++) {
            sb.append(grid[--i][j]);
        }
        if (--col == 0) {
            break;
        }
    }
    return sb.toString();
}
  1. Largest Sum Sub Array
    Find the largest sum contiguous sub array. The length of the returned sub array must beat least
    of length 2.
public static int largestSumSubArray (int[] arr) {
        if (arr == null || arr.length < 2) throw new IllegalArgumentException("Invalid Input");
        int firstSum = 0;
        int secondSum = 0;
        int max = Integer.MIN_VALUE;
        
        for (int i = 0; i < arr.length - 1; i++) {
            firstSum = Math.max(firstSum + arr[i], arr[i]);
            secondSum = firstSum + arr[i+1];
            max = Math.max(max, secondSum);
        }
        return max;
    }
public static String largetSumArray (int[] input) {
    if (input == null || input.length < 2) {
        throw new IllegalArgumentException();
    }
    int firstSum = input[0];
    int secondSum = firstSum + input[1];
    String maxStr = "" + input[0] + input[1];
    String currStr = "" + input[0];
    int max = input[0] + input[1];
    for (int i = 1; i < input.length - 1; i++) {
        int temp = input[i];
        if (firstSum + temp > temp) {
            currStr += " " + temp;
            firstSum += temp;
        } else {
            currStr = "" + temp;
            firstSum = temp;
        }
        secondSum = firstSum + input[i+1];
        if (secondSum > max) {
            max = secondSum;
            maxStr = currStr + " " + input[i+1];
        }
    }
    return maxStr; 
}
  1. Advisered Average Number
    Write a program to display the advisered average for the list of numbers my omitting the three
    largest number in the series.
    E.g:3,6,12,55,289,600,534,900,172. avg=(3+6+12+55+289+172) /6 and
    eliminating534,900,600
public static double adviseredNum (int[] arr) {
    if (arr == null || arr.length < 3) throw new IllegalArgumentException("input array is null or length is less than 3.");
    if (arr.length == 3) return 0;
    int maxOne = Integer.MIN_VALUE + 2;
    int maxTwo = Integer.MIN_VALUE + 1;
    int maxThree = Integer.MIN_VALUE;
    double sum = 0; 

    for (int i = 0; i < arr.length; i++) {
        int curr = arr[i];
        sum += curr;
        if (curr > maxThree) {
            if (curr > maxTwo) {
                if (curr > maxOne) {
                    maxThree = maxTwo;
                    maxTwo = maxOne;
                    maxOne = curr;
                } else {
                    maxThree = maxTwo;
                    maxTwo = curr;
                }
            } else {
                maxThree = curr;
            }
        }
    }
    return (sum - maxOne - maxTwo - maxThree) / (arr.length - 3);
}

//注意有÷, 结果最好是double的形式。
  1. Count And Say
    First, let user input a number, say 1. Then, the function will generate the next 10numbers
    which satisfy this condition:
    1, 11,21,1211,111221,312211…
    explanation: first number 1, second number is one 1, so 11. Third number is two1(previous
    number), so 21. next number one 2 one 1, so 1211 and so on…
public static String countAndSay (char oneChar){
        StringBuilder sb = new StringBuilder();
        sb.append(oneChar);
        for (int i = 0; i < 10; i++) {
            StringBuilder tempSb = new StringBuilder();
            int j = 0;
            while (j < sb.length()) {
                int start = j;
                char curr = sb.charAt(j);
                int len = 1;
                while (j + len < sb.length() && curr == sb.charAt(j+len)) {
                    len++;
                }
                j += len; 
                char b = (char) (len + '0');
                tempSb.append(b);
                tempSb.append((char) (curr));
            }
            sb = tempSb;
        }
        return sb.toString(); 
    }
//StringBuilder.append(char); sb.length(); sb.charAt(i); char c = (char) (# + ‘0’);
  1. Valid Password
    In 1-9 keypad one key is not working. If someone enters a password then not working key
    will not be entered. You have given expected password and entered password. Check that
    entered password is valid or not. Ex: entered 164, expected 18684 (you need to take care
    as when u enter18684 and 164 only both will be taken as 164 input)
public static boolean isValidPassword (String exp, String ent) {
    if (exp == ent) return true;
    if (exp.length() <= ent.length()) return false;
    int i = 0;
    int j = 0;
    int notWork = -1;

    while (i < exp.length() && j < ent.length()) {
        if (exp.charAt(i) == ent.charAt(j)) {
            i++;
            j++;
        }
        else {
            if (notWork == -1) {
                notWork = (int) (exp.charAt(i++) - '0');
            } else {
                if (notWork != (int) (exp.charAt(i) - '0')) {
                    return false; 
                } else {
                    i++;
                }
            }
        }
    }
    if (i == exp.length() && j == ent.length()) {
        return true;
    }
    return false; 
}
  1. Verify Password
    Verify if the given password is valid/invalid
  2. must be 5-12 characters long
  3. must contain at least one number and one lowercase character
  4. a sequence must not be followed by the same sequence (like 123123qs is invalid,
    123qs123 is valid)
public static boolean validPassword (String pw) {
    if (pw.length() < 5 && pw.length() > 12) {
        return false;
    }
    boolean oneLower = false;
    boolean oneNum = false;
    HashMap<Character, List<Integer>> hm = new HashMap<>();
    for (int i = 0; i < pw.length(); i++) {
        if (Character.isDigit(pw.charAt(i))) {
            oneLower = true;
        }
        if (Character.isLowerCase(pw.charAt(i))) {
            oneNum = true; 
        }
        List<Integer> temp = new ArrayList<>();
        if (hm.containsKey(pw.charAt(i))) {
            temp = hm.get(pw.charAt(i));
              temp.add(i);
        } else {
            temp.add(i);
        }
        hm.put(pw.charAt(i), temp);
    }
    if (!( oneLower && oneNum)) {
        return false; 
    }
 
    for (char key: hm.keySet()) {
        List<Integer> list = hm.get(key);
        for (int i = 0; i < list.size() - 1; i++) {
            int curr = list.get(i);
            int next = list.get(i+1);
            System.out.println(curr + " " + next);
            if (next + (next - curr) > pw.length()) {
                break;
            }
            String firstPart = pw.substring(curr, next);
            String secondPart = pw.substring(next, (2 * next-curr));
            if (firstPart.equals(secondPart)) {
                return false; 
            }
        }
    }
    return true; 
}
  1. Mountain Point
    Given a M * N matrix, if the element in the matrix is larger than other 8 elements who stay
    around it, then named that element be mountain point. Print all the mountain points.
public static List<String> mountainPoint (int[][] grid) {
    if (grid == null || grid.length < 3 || grid[0].length < 3) {
        throw new IllegalArgumentException();
    }
    int row = grid.length;
    int col = grid[0].length;
    List<String> result = new ArrayList<>();
    for (int i = 1; i < row - 1; i++) {
        for (int j = 1; j < col - 1; j++) {
            if (isMountainPoint(i, j, grid)) {
                result.add("(" + i + ", " + j + ")");
            }
        }
    }
    return result; 
}

private static boolean isMountainPoint (int i, int j, int[][] grid) {
    for (int x = i - 1; x <= i + 1; x++) {
        for (int y = j - 1; y < j + 1; y++) {
            if (x == i && y == j) {
                continue; 
            }
            if (grid[x][y] >= grid[i][j]) {
                return false;
            }
        }
    }
    return true; 
}
  1. Fibbonaci Number
    There is one kind of numbers call Fibbonaci Number, which satisfy the following situation:
    A. can be spilt into several numbers;
    B. The first two numbers are the same, the next number is equal to the sum of previous
    two eg. 112 (2 = 1 + 1), 12,122,436(12 + 12 = 24,12 + 24 = 36)
    If you are given a range by the user, find all numbers that are Fibbonaci numbers.
class Ideone
{
        public static List<Integer> fibbonaci (int start, int end) { // check each number from start to end, if it is fibbonaci, add it to list. 
              List<Integer> result = new ArrayList<>();
            if (end <=  start || end < 112) return result;
                                   If (start < 112) start = 112;
            for (int i = start; i <= end; i++) {
                if (isFibbonaci(i)) {
                    result.add(i);
                }
            }
            return result; 
        }
    
    private static boolean isFibbonaci (int num) {
        String numStr = Integer.toString(num);
        for (int i = 1; i <= numStr.length() / 3; i++) {
            String first = numStr.substring(0, i);
            String second = numStr.substring(i, 2*i);
            if (!first.equals(second)) continue;//additive和这不同,additive判断第一个数字比第二个数字小
            if (helper(first, second, numStr)) return true; 
        }
        return false;
    }
    
    private static boolean helper(String first, String second, String s) {
        int thirdNum = (int) (Integer.parseInt(first) + Integer.parseInt(second));
        String third = Integer.toString(thirdNum);
        int point = first.length() + second.length();
        if (s.length() - point < third.length()) return false;
        if (!third.equals(s.substring(point, point + third.length()))) return false;
        if (point + third.length() == s.length()) {
            return true; 
        } //记得判断
        return helper(second, third, s.substring(first.length(), s.length()));
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        List<Integer> list = fibbonaci(112, 11111);
        for (Integer i: list)
            System.out.println(i);
    }
}
  1. Coin Change
    Something cost $10.25 and the customer pays with a $20 bill, the program will print out the
    most efficient “change-breakdown” which is 1 five, 4 ones, and 3quarters. Find the minimum
    number of coins required to make change for a given sum (given unlimited number of N
    different denominations coin)
public static void coinChange (double pay, double cost) {
    if (cost > pay) {
        throw new IllegalArgumentException();
    }
    double change = pay - cost;
    int[] bill = {100, 50, 20, 10, 5, 1};
    int[] coin = {50, 25, 10, 5, 1};
    int billChange = (int) change / 1;
    int coinChange = (int) (change * 100) % 100;
    int[] billQty = new int[bill.length];
    int[] coinQty = new int[coin.length];
    helper(coin, coinQty, coinChange);
    helper(bill, billQty, billChange);
    for (int i = 0; i < bill.length; i++) {
        System.out.print(billQty[i] + " " + bill[i] + ", ");
    }
    System.out.println();
    for (int j = 0; j < coin.length; j++) {
        System.out.print(coinQty[j] + " " + coin[j] + ", ");
    }
} 

private static void helper (int[] pattern, int[] qty, int change) {
    for (int i = 0; i < pattern.length; i++) {
        if (change / pattern[i] != 0) {
            qty[i] = change / pattern[i];
            change %= pattern[i];
        }
    }
}

  1. Separate the number
    Print the sequences from the input given by the user separated by semicolon
    e.g.: 4678912356012356
    output: 4;6789;123;56;0123;56;
public static String separateNum (String numStr) {
    StringBuilder sb = new StringBuilder();
    int i = 0;
    while (i < numStr.length()) {
        char curr = numStr.charAt(i);
        int len = 1;
        while (i + len < numStr.length()) {
            if (numStr.charAt(i+len) == (char) (curr + len) ) {
                len++;
            } else {
                break;
            }
        }
        sb.append(numStr.substring(i, i + len));
        sb.append(";");
        i += len;
    }
    return sb.toString();
}
  1. Swapping String
    Given two strings, you need to transpose the first string to the second string by means of
    only swaps between 2 consecutive characters in the first string. This must be performed by
    doing a series of these swaps in order to get the second string.
public static void swapString (String origin, String pattern) {
    if (origin == null || pattern == null || origin.length() != pattern.length()) {
        throw new IllegalArgumentException();
    }
    char[] originChar = origin.toCharArray();
    char[] patternChar = pattern.toCharArray();
    for (int i = 0; i < pattern.length(); i++) {
        if (originChar[i] == patternChar[i]) {
            continue;
        } else {
                                   If (i == patternChar.length - 1) throw new IllegalArgumentException();
            helper(originChar, i, patternChar[i]);            
        }
    }
}

private static void helper (char[] origin, int pos, char target) {
    int j = pos + 1;
    while (j < origin.length) {
        if (origin[j] == target) {
            break;    
        }
        j++;
    }
    if (j == origin.length) {
        throw new IllegalArgumentException();
    }
    swap(pos, j, origin);
}

private static void swap (int pos, int i, char[] origin) {
    while (pos != i) {
        char temp = origin[i];
        origin[i] = origin[i-1];
        origin[i-1] = temp;
        i--;
    }    
    System.out.println(String.valueOf(origin));
}