Build solutions that give people the power to build community and bring the world closer together.

https://devcommunitychallenge.devpost.com/

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