690. Employee Importance

690. Employee Importance

/*
// Employee info
class Employee {
    // It's the unique id of each node;
    // unique id of this employee
    public int id;
    // the importance value of this employee
    public int importance;
    // the id of direct subordinates
    public List subordinates;
};
*/
class Solution {
	public Employee getEmployeeById(List employees, int id1) {
		for (int i = 0; i < employees.size(); i++) {
			if (employees.get(i).id == id1)
				return employees.get(i);
		}
		return null;
	}

	public int getImportance(List employees, int id) {
		int result = 0;
		Employee current = getEmployeeById(employees, id);
		result += current.importance;

		if (current.subordinates == null || current.subordinates.size() == 0)
			return result;

		for (int i = 0; i < current.subordinates.size(); i++)
			result += getImportance(employees, current.subordinates.get(i));

		return result;

	}
}

520. Detect Capital

520. Detect Capital

class Solution {
    	public boolean detectCapitalUse(String word) {
		if (Character.isLowerCase(word.charAt(0))) {
			if (isAllLowerCase(word.substring(1)))
				return true;
		} else {
			if (isAllLowerCase(word.substring(1)) || isAllUpperCase(word.substring(1)))
				return true;
		}
		return false;
	}

	public boolean isAllLowerCase(String word) {
		for (int i = 0; i < word.length(); i++) {
			if (Character.isUpperCase(word.charAt(i)))
				return false;
		}
		return true;
	}

	public boolean isAllUpperCase(String word) {
		for (int i = 0; i < word.length(); i++) {
			if (!Character.isUpperCase(word.charAt(i)))
				return false;
		}
		return true;
	}
}

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;
	}
}