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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s