796. Rotate String

796. Rotate String

class Solution {
 public boolean rotateString(String A, String B) {
	     return rotateString( A,  B , 0);
	    }

	private boolean rotateString(String a, String b, int i) {
        if( a.length() <=1 && b.length()  b.length()) return false;
		if( a.equals(b)) return true;

 			return rotateString( a.substring(1)+a.charAt(0), b, i+1);
		}
}
Advertisements

784. Letter Case Permutation

784. Letter Case Permutation

class Solution {
    public static List letterCasePermutation(String S) {
		List list = new ArrayList();
		letterCase(S, 0, list);
		return list;
	}

	private static void letterCase(String s, int i, List list) {
		if (i == s.length()) {
			list.add(s);
			return;
		}
		char a = s.charAt(i);
		if (Character.isDigit(a)) {
			letterCase(s, i + 1, list);
		} else {
			if (Character.isUpperCase(a)) {
				letterCase(s.substring(0, i) + Character.toLowerCase(a) + s.substring(i + 1), i + 1, list);
				letterCase(s.substring(0, i) + a + s.substring(i + 1), i + 1, list);
			} else if (Character.isLowerCase(a)) {
				letterCase(s.substring(0, i) + Character.toUpperCase(a) + s.substring(i + 1), i + 1, list);
				letterCase(s.substring(0, i) + a + s.substring(i + 1), i + 1, list);
			}
		}
	}
}

762. Prime Number of Set Bits in Binary Representation

762. Prime Number of Set Bits in Binary Representation

class Solution {
   	boolean isPrime(int n) {
        if( n ==1 ) return false;
		for (int i = 2; i < n; i++) {
			if (n % i == 0)
				return false;
		}
		return true;
	}

	public int countPrimeSetBits(int L, int R) {
		int count = 0;
		for (int i = L; i <= R; i++) {
			String binary = Integer.toBinaryString(i);
			int setBits = 0;
			for (int j = 0; j < binary.length(); j++)
				if (binary.charAt(j) == '1')
					setBits++;
			if (isPrime(setBits))
				count++;
		}
		return count;
	}
}

566. Reshape the Matrix

566. Reshape the Matrix

class Solution {
	public int[][] matrixReshape(int[][] nums, int r, int c) {
		if (nums.length * nums[0].length != r * c)
			return nums;
		int[][] out = new int[r][c];

		boolean[][] visited = new boolean[r][c];

		for (int i = 0; i < nums.length; i++)
			for (int j = 0; j < nums[i].length; j++) {
				searchEmptyPlace: {
					for (int a = 0; a < out.length; a++)
						for (int b = 0; b < out[a].length; b++)
							if (!visited[a][b]) {
								out[a][b] = nums[i][j];
								visited[a][b] = true;
								break searchEmptyPlace;
							}
				}
			}

		return out;
	}
}

824. Goat Latin

824. Goat Latin

class Solution {
public String toGoatLatin(String S) {
		StringBuilder out = new StringBuilder();

		String[] words = S.split(" ");

		for (int i = 0; i < words.length; i++) {

			if (words[i].charAt(0) == 'a' || words[i].charAt(0) == 'e' || words[i].charAt(0) == 'i'
					|| words[i].charAt(0) == 'o' || words[i].charAt(0) == 'u' || words[i].charAt(0) == 'A'
					|| words[i].charAt(0) == 'E' || words[i].charAt(0) == 'I' || words[i].charAt(0) == 'O'
					|| words[i].charAt(0) == 'U') {
				words[i] += "ma";
			} else {
				words[i] = words[i].substring(1) + words[i].charAt(0)+ "ma";
			}

			for (int j = 1; j <= i + 1; j++)
				words[i] += "a";
			if (i == words.length - 1)// end of sentence
				out.append(words[i]);
			else
				out.append(words[i] + " ");
		}

		return out.toString();
	}
}

463. Island Perimeter

463. Island Perimeter

class Solution {
	public static int islandPerimeter(int[][] grid) {
		int perimeter = 0;
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid[i].length; j++) {
				if (grid[i][j] == 1) {
					if (i - 1 < 0 || grid[i - 1][j] == 0)
						perimeter++;
					if (j - 1 = grid.length || grid[i + 1][j] == 0)
						perimeter++;
					if (j + 1 >= grid[i].length || grid[i][j + 1] == 0)
						perimeter++;
				}

			}
		}
		return perimeter;
	}
}

682. Baseball Game

682. Baseball Game

class Solution {
			public int calPoints(String[] ops) {
		int currentSum = 0;
		ArrayList valid = new ArrayList();
		for (int i = 0; i < ops.length; i++) {

			if (ops[i].equals("C")) {
				currentSum -= new Integer(valid.get(valid.size() - 1));
				valid.remove(valid.size() - 1);
			} else if (ops[i].equals("D")) {
				int newScore = (2 * valid.get(valid.size() - 1));
				currentSum += newScore;
				valid.add(newScore);
			} else if (ops[i].equals("+")) {
				int newScore = new Integer(valid.get(valid.size() - 1)) + new Integer((valid.get(valid.size() - 2)));
				currentSum += newScore;
				valid.add(newScore);
			} else {
				valid.add(new Integer(ops[i]));
				currentSum += new Integer(ops[i]);
			}
		}
		return currentSum;

	}
}