Migrate string to uppercase and lowercase

I have a line, "abc". What will a program (if possible, in Java) look like that rearranges a string?

For example:

abc ABC Abc aBc abC ABc abC AbC 
+7
source share
6 answers

Something like this should do the trick:

 void printPermutations(String text) { char[] chars = text.toCharArray(); for (int i = 0, n = (int) Math.pow(2, chars.length); i < n; i++) { char[] permutation = new char[chars.length]; for (int j =0; j < chars.length; j++) { permutation[j] = (isBitSet(i, j)) ? Character.toUpperCase(chars[j]) : chars[j]; } System.out.println(permutation); } } boolean isBitSet(int n, int offset) { return (n >> offset & 1) != 0; } 
+12
source

As you probably already know, the number of possible different combinations is 2 ^ n, where n is equal to the length of the input string.

Since n can theoretically be quite large, it is likely that 2 ^ n will exceed the capacity of a primitive type such as int. (The user may have to wait a few years for all combinations to finish printing, but this is their business.)

Instead, use bit-bit to save all possible combinations. We set the number of bits to n and initialize them all to 1. For example, if the input string is "abcdefghij", the initial values โ€‹โ€‹of the bit vector will be {1111111111}.

For each combination, we just have to scroll through all the characters in the input line and set each of them to uppercase if the corresponding bit is 1, otherwise set it to lowercase. Then we reduce the bit vector and repeat.

For example, the process would look like this for entering "abc":

Bits: Matching Combo:
111 ABC
110 ABc
101 AbC
100 abc
011 AbC
010 AbC
001 AbC
000 a

By using a loop rather than calling a recursive function, we also avoid throwing exceptions on large input strings.

Here is the real implementation:

 import java.util.BitSet; public void PrintCombinations(String input) { char[] currentCombo = input.toCharArray(); // Create a bit vector the same length as the input, and set all of the bits to 1 BitSet bv = new BitSet(input.length()); bv.set(0, currentCombo.length); // While the bit vector still has some bits set while(!bv.isEmpty()) { // Loop through the array of characters and set each one to uppercase or lowercase, // depending on whether its corresponding bit is set for(int i = 0; i < currentCombo.length; ++i) { if(bv.get(i)) // If the bit is set currentCombo[i] = Character.toUpperCase(currentCombo[i]); else currentCombo[i] = Character.toLowerCase(currentCombo[i]); } // Print the current combination System.out.println(currentCombo); // Decrement the bit vector DecrementBitVector(bv, currentCombo.length); } // Now the bit vector contains all zeroes, which corresponds to all of the letters being lowercase. // Simply print the input as lowercase for the final combination System.out.println(input.toLowerCase()); } public void DecrementBitVector(BitSet bv, int numberOfBits) { int currentBit = numberOfBits - 1; while(currentBit >= 0) { bv.flip(currentBit); // If the bit became a 0 when we flipped it, then we're done. // Otherwise we have to continue flipping bits if(!bv.get(currentBit)) break; currentBit--; } } 
+6
source
 String str = "Abc"; str = str.toLowerCase(); int numOfCombos = 1 << str.length(); for (int i = 0; i < numOfCombos; i++) { char[] combinations = str.toCharArray(); for (int j = 0; j < str.length(); j++) { if (((i >> j) & 1) == 1 ) { combinations[j] = Character.toUpperCase(str.charAt(j)); } } System.out.println(new String(combinations)); } 
+4
source

I'm not sure if this will work since I have not used Java for more than three years, but first wrote it in PHP and worked. It returns an array containing all permuted rows. This is a recursive method:

 public String[] permute(String s){ String[] returnArray; if(s.length() == 1){ returnArray = new String[2]; returnArray[0] = s.toUpperCase(); returnArray[1] = s.toLowerCase(); return returnArray; } String[] permutedArray = permute(s.substring(1)); returnArray = new String[permutedArray.length*2]; for(int i = 0; i < permutedArray.length; i++){ returnArray[i*2] = s.substring(0,1).toUpperCase()+permutedArray[$i]; returnArray[i*2+1] = s.substring(0,1).toLowerCase()+permutedArray[$i]; } return returnArray; } 

If you're curious, PHP, which I know works:

 function permute($s){ if(strlen($s) == 1) return array(strtoupper($s), strtolower($s)); $arr = permute(substr($s,1)); for($i = 0; $i < count($arr); $i++){ $newArr[$i*2] = strtoupper(substr($s,0,1)).$arr[$i]; $newArr[$i*2+1] = strtolower(substr($s,0,1)).$arr[$i]; } return $newArr; } 
0
source

Here you will find the code snippet for the above:

 public class StringPerm { public static void main(String[] args) { String str = "abc"; String[] f = permute(str); for (int x = 0; x < f.length; x++) { System.out.println(f[x]); } } public static String[] permute(String str) { String low = str.toLowerCase(); String up = str.toUpperCase(); char[] l = low.toCharArray(); char u[] = up.toCharArray(); String[] f = new String[10]; f[0] = low; f[1] = up; int k = 2; char[] temp = new char[low.length()]; for (int i = 0; i < l.length; i++) { temp[i] = l[i]; for (int j = 0; j < u.length; j++) { if (i != j) { temp[j] = u[j]; } } f[k] = new String(temp); k++; } for (int i = 0; i < u.length; i++) { temp[i] = u[i]; for (int j = 0; j < l.length; j++) { if (i != j) { temp[j] = l[j]; } } f[k] = new String(temp); k++; } return f; } 

}

0
source

You can do something like

`` ``

 import java.util.*; public class MyClass { public static void main(String args[]) { String n=(args[0]); HashSet<String>rs = new HashSet(); helper(rs,n,0,n.length()-1); System.out.println(rs); } public static void helper(HashSet<String>rs,String res , int l, int n) { if(l>n) return; for(int i=l;i<=n;i++) { res=swap(res,i); rs.add(res); helper(rs,res,l+1,n); res=swap(res,i); } } public static String swap(String st,int i) { char c = st.charAt(i); char ch[]=st.toCharArray(); if(Character.isUpperCase(c)) { c=Character.toLowerCase(c); } else if(Character.isLowerCase(c)) { c=Character.toUpperCase(c); } ch[i]=c; return new String(ch); } } 

`` ``

0
source

All Articles