Related
For example:
I have a string mask with length = 5 symbols and I have a value with length = 3 symbols.
All combinations are:
val__, _val_, __val
another example for mask length = 3, value length = 2:
xx_, _xx
How to generate these masks programmatically?
For example in method with following signature:
String[] generateMasks(int maskLength, String val);
My attempts:
private ArrayList<String> maskGenerator2(int from, char[] value) {
ArrayList<String> result = new ArrayList<String>();
//initial position
char[] firstArray = new char[from];
for (int i=0; i<from; i++) {
if (i < value.length) firstArray[i] = value[i];
else firstArray[i] = '_';
}
result.add(String.valueOf(firstArray));
System.out.println(firstArray);
//move value
int k = 0;
while (k < from - value.length) {
char last = firstArray[from - 1];
for (int i = from - 1; i > 0; i--) {
firstArray[i] = firstArray[i - 1];
}
firstArray[0] = last;
result.add(String.valueOf(firstArray));
System.out.println(firstArray);
k++;
}
return result;
}
Maybe are there more elegant solutions for this?
Create a String consisting of repeated mask symbols to required length .. see Simple way to repeat a String in java
Use StringBuilder to insert the input string at each possible point.
Example
public static void main(String[] args) throws Exception {
System.out.println(createMask(5, "val"));
System.out.println(createMask(3, "xx"));
}
private static List<String> createMask(int length, String value) {
List<String> list = new ArrayList<String>();
String base = new String(new char[length]).replace("\0", "_");
for (int offset = 0; offset <= length - value.length(); offset++) {
StringBuilder buffer = new StringBuilder(base);
buffer.replace(offset, offset + value.length(), value);
list.add(buffer.toString());
}
return list;
}
Output
[val__, _val_, __val]
[xx_, _xx]
This is a cipher I wrote in Java but I don't know if it works correctly because when I pass a file such as a .gif it produces a smaller output string and when I convert it back the gif doesn't work and it is smaller by a few bytes than the original. I think it maybe be a problem with the binary to ascii conversion. Does anyone know how to fix it?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
public class XOR {
public static void main(String args[]) throws UnsupportedEncodingException, FileNotFoundException {
String data = "this is a string";
String rawKey = "this is a key.".toLowerCase();
//Checks the key is valid.
if (rawKey.length() % 2 != 0) {
System.err.println("The key has to be an even length.");
System.exit(1);
}
for(int p = 0; p < rawKey.length()-1; p++){
if (rawKey.charAt(p) == rawKey.charAt(p+1)) {
System.err.println("The key can't have repeated characters next to each other.");
System.exit(1);
}
}
long dataLength = data.length();
String content = strToBin(data);
String working = content;
for (int i = 0; i < rawKey.length(); i++) {
working = XOR(working, String.valueOf(strToBin(String.valueOf(rawKey.charAt(i)))), 8, dataLength);
}
System.out.println(working);
System.out.println(binToStr(working));
for (int i = 0; i < rawKey.length(); i++) {
working = XOR(working, String.valueOf(strToBin(String.valueOf(rawKey.charAt(i)))), 8, dataLength);
}
System.out.println(binToStr(working));
}
public static String XOR(String input, String key, int blockSize, long length) {
long tries = length;
String original = input; //INPUTS MUST BE IN BINARY
String compare = key; //INPUTS MUST BE IN BINARY
String build = "";
long o = 0; //The offset so we skip characters we have already done.
for (int t = 0; t < tries; t++) { //Do each block.
for(int i = 0; i < blockSize; i++) { //Do each block one at a time.
if (original.charAt((int) (i+o)) == compare.charAt(i)) { //Compares the block to the key
build += "0"; //If the key and the character are the same false.
} else {
build += "1"; //If the key and the character are the different true.
}
}
o += blockSize+1; //+1 to include the space.
if (t != tries-1) {build += " ";}; //Adds space. If statement prevents adding a pointless space onto the end.
}
return build;
}
public static String strToBin(String input) {
byte[] bytes = input.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.append(' ');
}
return binary.toString();
}
public static String binToStr( String s ) {
String[] ss = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ss.length; i++) {
sb.append( (char)Integer.parseInt( ss[i], 2 ) );
}
return sb.toString();
}
}
I followed the Rosetta Java code implementation.
I tried do this LZW coding with my own Dictionary and not with the ASCII Dictionary which was used.
When I try with my own Dictioanry there is a problem about decoding... The result is wrong, because each of decoded word don't view the first 'a' letter.
The result have to be 'abraca abrac abra' and not 'braca brac bra'
I see the problem in decode() method at String act = "" + (char)(int)compressed.remove(0); This will remove all first 'a' letter.
But I don't have any ideas how can I modify this line...
For example if I use the String act = "";instead of above line... the coding will be very wrong, or use another command... I don't know how can I solve this little problem... Or maybe I am looking for on the bad way for the solution.
public class LZW {
public static List<Integer> encode(String uncompressed) {
Map<String,Integer> dictionary = DictionaryInitStringInt();
int dictSize = dictionary.size();
String act = "";
List<Integer> result = new ArrayList<Integer>();
for (char c : uncompressed.toCharArray()) {
String next = act + c;
if (dictionary.containsKey(next))
act = next;
else {
result.add(dictionary.get(act));
// Add next to the dictionary.
dictionary.put(next, dictSize++);
act = "" + c;
}
}
// Output the code for act.
if (!act.equals(""))
result.add(dictionary.get(act));
return result;
}
public static String decode(List<Integer> compressed) {
Map<Integer,String> dictionary = DictionaryInitIntString();
int dictSize = dictionary.size();
String act = "" + (char)(int)compressed.remove(0);
//String act = "";
String result = act;
for (int k : compressed) {
String entry;
if (dictionary.containsKey(k))
entry = dictionary.get(k);
else if (k == dictSize)
entry = act + act.charAt(0);
else
throw new IllegalArgumentException("Nincs ilyen kulcs: " + k);
result += entry;
dictionary.put(dictSize++, act + entry.charAt(0));
act = entry;
}
return result;
}
public static Map<String,Integer> DictionaryInitStringInt()
{
char[] characters = {'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',' ','!',
'?','.',','};
int charactersLength = characters.length;
Map<String,Integer> dictionary = new HashMap<String,Integer>();
for (int i = 0; i < charactersLength; i++)
dictionary.put("" + characters[i], i);
return dictionary;
}
public static Map<Integer,String> DictionaryInitIntString()
{
char[] characters = {'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',' ','!',
'?','.',','};
int charactersLength = characters.length;
Map<Integer,String> dictionary = new HashMap<Integer,String>();
for (int i = 0; i < charactersLength; i++)
dictionary.put(i,"" + characters[i]);
return dictionary;
}
public static void main(String[] args) {
List<Integer> compressed = encode("abraca abrac abra");
System.out.println(compressed);
String decodeed = decode(compressed);
// decodeed will be 'braca brac bra'
System.out.println(decodeed);
}
}
The rosetta example use
"" + (char) (int) compressed.remove(0);
because the first 256 entries of the dictionnary map exactly the 'char' values.
With a custom dictionnary this line should be:
String act = dictionary.get(compressed.remove(0));
I'm trying to make a basic encryption program that used random numbers to encrypt the whole alphabet, encrypt a user submitted phrase and then decrypt it back to the original phrase but I am finding it very hard. Can anyone help point out my mistakes please! It shouldn't code two letters to the same letter, ie a and b shouldn't ever be both matched to c.
public class MainClass {
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public static final char[] Ualpha =
{'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
};
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];
Random rgenerator = new Random();
public MainClass(){
int randNum = rgenerator.nextInt(ALPHASIZE);
for(int i=0; i<ALPHASIZE ; i++)
{
//makes sure that it won't assign a letter to itself or to one that has already been assigned
do {
randNum = rgenerator.nextInt(26);
} while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));
encryptU[i] = Ualpha[randNum];
encryptL[i] = Lalpha[randNum];
decrypt[i] = randNum;
}
}
public String encrypt(String secret)
{
System.out.println(Arrays.toString(encryptU));
int position = 0;
char[] mess = secret.toCharArray();
for(int i = 0 ; i<mess.length;i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Ualpha[j]) {
position = j;
}
mess[i] = encryptU[position];
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j < encryptU.length; j++) {
if(mess[i]==Lalpha[j]) {
position = j;
}
mess[i] = encryptL[position];
}
}
}
return new String(mess);
}
public String decrypt(String secret)
{
char[] mess = secret.toCharArray();
for(int i = 0 ; i<mess.length;i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptU[j]){
mess[i] = Ualpha[j];
}
}
}
if(Character.isLowerCase(mess[i]))
{
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptL[j]){
mess[i] = Lalpha[j];
}
}
}
}
return new String(mess);
}
}
You should really consider using a Map to store character/encoding pairs. Oh, and to create these random pairs you can add your characters to a List and make use of Collections.shuffle instead of reinventing the wheel yourself.
Let me demonstrate using only Lalpha (only lowercase letters). You want something along these lines:
List<Character> l = new ArrayList<Character>(Lalpha.length);
for (char c : Lalpha)
l.add(c);
Collections.shuffle(l);
Map<Character, Character> encoding = new HashMap<Character, Character>(Lalpha.length);
Map<Character, Character> decoding = new HashMap<Character, Character>(Lalpha.length);
for (int i = 0 ; i < Lalpha.length ; i++) {
encoding.put(Lalpha[i], l.get(i));
decoding.put(l.get(i), Lalpha[i]);
}
Now lets say we wanted to encode / decode the string helloworld, we would do this:
String s = "helloworld";
// Encode:
String enc = "";
for (char c : s.toCharArray())
enc += encoding.get(c);
System.out.println(enc);
// Decode:
String dec = "";
for (char c : enc.toCharArray())
dec += decoding.get(c);
System.out.println(dec);
Output (one of many possible):
vjwwmtmcwz
helloworld
Of course, you can incorporate uppercase letters and what-not using the same idea.
It sounds like you need to generate a permutation of the allowed letters. This is how I would do it for lower case letters:
public char[] permutation =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public generatePermutation()
{
Random r = new Random();
char tmp;
int rand;
for(int i = 0; i < permutation.length; ++i)
{
rand = r.nextInt(permutation.length - i);
tmp = permutation[i];
permutation[i] = permutation[rand];
permutation[rand] = tmp;
}
}
Finally, you can access this array for encryption by doing permutation[inputChar-'a'] (assuming you've already made sure inputChar is a lower case letter). And for decryption you find the letter that matches your input char and add 'a' to the index.
If you're having problems creating a random mapping between your set of letters and an "encrypted" set, you could start with this:
List<Character> alphabet = new LinkedList<Character>(
new String[] {'a', 'b', ..., 'Y', 'Z'});
List<Character> shuffledAlphabet = new LinkedList<Character>(alphabet);
Collections.shuffle(shuffledAlphabet);
Map<Character, Character> encryptionMap = new HashMap<Character, Character>();
Map<Character, Character> decryptionMap = new HashMap<Character, Character>();
for (int i=0; i < alphabet.size(); i++) {
encryptionMap.put(alphabet.get(i), shuffledAlphabet.get(i));
decryptionMap.put(shuffledAlphabet.get(i), alphabet.get(i));
}
Now you encrypt by taking each character for the typed String, doing a get on the encryptionMap, and replacing with the result of that get. To decrypt, do the same thing with the decryptionMap.
I'm trying to find permutation of a given string, but I want to use iteration. The recursive solution I found online and I do understand it, but converting it to an iterative solution is really not working out. Below I have attached my code. I would really appreciate the help:
public static void combString(String s) {
char[] a = new char[s.length()];
//String temp = "";
for(int i = 0; i < s.length(); i++) {
a[i] = s.charAt(i);
}
for(int i = 0; i < s.length(); i++) {
String temp = "" + a[i];
for(int j = 0; j < s.length();j++) {
//int k = j;
if(i != j) {
System.out.println(j);
temp += s.substring(0,j) + s.substring(j+1,s.length());
}
}
System.out.println(temp);
}
}
Following up on my related question comment, here's a Java implementation that does what you want using the Counting QuickPerm Algorithm:
public static void combString(String s) {
// Print initial string, as only the alterations will be printed later
System.out.println(s);
char[] a = s.toCharArray();
int n = a.length;
int[] p = new int[n]; // Weight index control array initially all zeros. Of course, same size of the char array.
int i = 1; //Upper bound index. i.e: if string is "abc" then index i could be at "c"
while (i < n) {
if (p[i] < i) { //if the weight index is bigger or the same it means that we have already switched between these i,j (one iteration before).
int j = ((i % 2) == 0) ? 0 : p[i];//Lower bound index. i.e: if string is "abc" then j index will always be 0.
swap(a, i, j);
// Print current
System.out.println(join(a));
p[i]++; //Adding 1 to the specific weight that relates to the char array.
i = 1; //if i was 2 (for example), after the swap we now need to swap for i=1
}
else {
p[i] = 0;//Weight index will be zero because one iteration before, it was 1 (for example) to indicate that char array a[i] swapped.
i++;//i index will have the option to go forward in the char array for "longer swaps"
}
}
}
private static String join(char[] a) {
StringBuilder builder = new StringBuilder();
builder.append(a);
return builder.toString();
}
private static void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
List<String> results = new ArrayList<String>();
String test_str = "abcd";
char[] chars = test_str.toCharArray();
results.add(new String("" + chars[0]));
for(int j=1; j<chars.length; j++) {
char c = chars[j];
int cur_size = results.size();
//create new permutations combing char 'c' with each of the existing permutations
for(int i=cur_size-1; i>=0; i--) {
String str = results.remove(i);
for(int l=0; l<=str.length(); l++) {
results.add(str.substring(0,l) + c + str.substring(l));
}
}
}
System.out.println("Number of Permutations: " + results.size());
System.out.println(results);
Example:
if we have 3 character string e.g. "abc", we can form permuations as below.
1) construct a string with first character e.g. 'a' and store that in results.
char[] chars = test_str.toCharArray();
results.add(new String("" + chars[0]));
2) Now take next character in string (i.e. 'b') and insert that in all possible positions of previously contsructed strings in results. Since we have only one string in results ("a") at this point, doing so gives us 2 new strings 'ba', 'ab'. Insert these newly constructed strings in results and remove "a".
for(int i=cur_size-1; i>=0; i--) {
String str = results.remove(i);
for(int l=0; l<=str.length(); l++) {
results.add(str.substring(0,l) + c + str.substring(l));
}
}
3) Repeat 2) for every character in the given string.
for(int j=1; j<chars.length; j++) {
char c = chars[j];
....
....
}
This gives us "cba", "bca", "bac" from "ba" and "cab", "acb" and "abc" from "ab"
Work queue allows us to create an elegant iterative solution for this problem.
static List<String> permutations(String string) {
List<String> permutations = new LinkedList<>();
Deque<WorkUnit> workQueue = new LinkedList<>();
// We need to permutate the whole string and haven't done anything yet.
workQueue.add(new WorkUnit(string, ""));
while (!workQueue.isEmpty()) { // Do we still have any work?
WorkUnit work = workQueue.poll();
// Permutate each character.
for (int i = 0; i < work.todo.length(); i++) {
String permutation = work.done + work.todo.charAt(i);
// Did we already build a complete permutation?
if (permutation.length() == string.length()) {
permutations.add(permutation);
} else {
// Otherwise what characters are left?
String stillTodo = work.todo.substring(0, i) + work.todo.substring(i + 1);
workQueue.add(new WorkUnit(stillTodo, permutation));
}
}
}
return permutations;
}
A helper class to hold partial results is very simple.
/**
* Immutable unit of work
*/
class WorkUnit {
final String todo;
final String done;
WorkUnit(String todo, String done) {
this.todo = todo;
this.done = done;
}
}
You can test the above piece of code by wrapping them in this class.
import java.util.*;
public class AllPermutations {
public static void main(String... args) {
String str = args[0];
System.out.println(permutations(str));
}
static List<String> permutations(String string) {
...
}
}
class WorkUnit {
...
}
Try it by compiling and running.
$ javac AllPermutations.java; java AllPermutations abcd
The below implementation can also be easily tweaked to return a list of permutations in reverse order by using a LIFO stack of work instead of a FIFO queue.
import java.util.List;
import java.util.Set;
import java.util.ArrayList;
import java.util.HashSet;
public class Anagrams{
public static void main(String[] args)
{
String inpString = "abcd";
Set<String> combs = getAllCombs(inpString);
for(String comb : combs)
{
System.out.println(comb);
}
}
private static Set<String> getAllCombs(String inpString)
{
Set<String> combs = new HashSet<String>();
if( inpString == null | inpString.isEmpty())
return combs;
combs.add(inpString.substring(0,1));
Set<String> tempCombs = new HashSet<String>();
for(char a : inpString.substring(1).toCharArray())
{
tempCombs.clear();
tempCombs.addAll(combs);
combs.clear();
for(String comb : tempCombs)
{
combs.addAll(getCombs(comb,a));
}
}
return combs;
}
private static Set<String> getCombs(String comb, char a) {
Set<String> combs = new HashSet<String>();
for(int i = 0 ; i <= comb.length(); i++)
{
String temp = comb.substring(0, i) + a + comb.substring(i);
combs.add(temp);
//System.out.println(temp);
}
return combs;
}
}
Just posting my approach to the problem:
import java.util.ArrayDeque;
import java.util.Queue;
public class PermutationIterative {
public static void main(String[] args) {
permutationIterative("abcd");
}
private static void permutationIterative(String str) {
Queue<String> currentQueue = null;
int charNumber = 1;
for (char c : str.toCharArray()) {
if (currentQueue == null) {
currentQueue = new ArrayDeque<>(1);
currentQueue.add(String.valueOf(c));
} else {
int currentQueueSize = currentQueue.size();
int numElements = currentQueueSize * charNumber;
Queue<String> nextQueue = new ArrayDeque<>(numElements);
for (int i = 0; i < currentQueueSize; i++) {
String tempString = currentQueue.remove();
for (int j = 0; j < charNumber; j++) {
int n = tempString.length();
nextQueue.add(tempString.substring(0, j) + c + tempString.substring(j, n));
}
}
currentQueue = nextQueue;
}
charNumber++;
}
System.out.println(currentQueue);
}
}
package vishal villa;
import java.util.Scanner;
public class Permutation {
static void result( String st, String ans)
{
if(st.length() == 0)
System.out.println(ans +" ");
for(int i = 0; i<st.length(); i++)
{
char ch = st.charAt(i);
String r = st.substring(0, i) + st.substring(i + 1);
result(r, ans + ch);
}
}
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
System.out.println("enter the string");
String st = Sc.nextLine();
Permutation p = new Permutation();
p.result(st,"" );
}
}
// Java program to print all permutations of a
// given string.
public class Permutation
{
public static void main(String[] args)
{
String str = "ABC";
int n = str.length();
Permutation permutation = new Permutation();
permutation.permute(str, 0, n-1);
}
/**
* permutation function
* #param str string to calculate permutation for
* #param s starting index
* #param e end index
*/
private void permute(String str, int s, int e)
{
if (s == e)
System.out.println(str);
else
{
for (int i = s; i <= s; i++)
{
str = swap(str,l,i);
permute(str, s+1, e);
str = swap(str,l,i);
}
}
}
/**
* Swap Characters at position
* #param a string value
* #param i position 1
* #param j position 2
* #return swapped string
*/
public String swap(String a, int i, int j)
{
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
}