-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathPermuteCasewise.java
44 lines (39 loc) · 1 KB
/
PermuteCasewise.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Facebook interview question
*
* Problem: Permute the String including case
*
* For example: "abc" = > abc, ABC, Abc, aBc, abC, ABc, abC, AbC
*
* Solution: If N be the string length of input then there exists 2^N maximum combinations
*
* Represent this as a bitwise operation of size input.length() which is 3
*
* 0 0 0 => 0
* 1 0 0 => Convert 0th char to upper case
* 1 1 0 => Convert 0, 1 char to upper case and so on
*
*/
public class PermuteCasewise
{
public void generatePermute(String input)
{
int max = 1 << input.length();
for(int i=0; i<max; i++)
{
input = input.toLowerCase();
char [] combination = input.toCharArray();
for(int j=0; j<input.length(); j++)
{
if( ((i >> j) & 1) == 1 )
combination[j] = Character.toUpperCase(input.charAt(j));
}
System.out.println(String.valueOf(combination));
}
}
public static void main(String[] args)
{
PermuteCasewise obj = new PermuteCasewise();
obj.generatePermute("aBc");
}
}