• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

Java String Mutations

I am trying to create a java program that will take a jumbled string of various length, and solve it. The idea I have to solve it, is to create all the mutations or variations of the input string and match them to a dictionary array list I already have created. I do not know the easiest implementation of creating all of the mutations of this string. Any help would be greatly appreciated.
0
robo_doc
Asked:
robo_doc
  • 4
  • 2
1 Solution
 
for_yanCommented:

Indeed works from the example above:

import java.util.ArrayList;
import java.util.List;


public class Permutations {
    public static void main(String [] args){
        System.out.println(permute("1234"));

    }

  public static  List<String> permute(String s) {
        List<String> permutations = new ArrayList<String>();
        if (s.length() == 0) {
            permutations.add("");
            return permutations;
        }
        char firstChar = s.charAt(0);
        String restOfString = s.substring(1);
        List<String> words = permute(restOfString);
        for (String word : words) {
            for (int i = 0; i <= word.length(); i++)
                permutations.add(insertCharAt(word, firstChar, i));
        }
        return permutations;
    }

   static String insertCharAt(String word, char c, int i) {
        String start = word.substring(0, i);
        String end = word.substring(i);
        return start + c + end;
    }

}

Open in new window


Output:

[1234, 2134, 2314, 2341, 1324, 3124, 3214, 3241, 1342, 3142, 3412, 3421, 1243, 2143, 2413, 2431, 1423, 4123, 4213, 4231, 1432, 4132, 4312, 4321]

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
robo_docAuthor Commented:
The example above works, now could you help me with taking a word list text file, hashing each word (they are all on separate lines) mapping these words and the keys generated for them, and then searching through it to find if the mutations generated are present in the list?

I thought I had the code implemented correctly, but it does not utilize any sort of hashing, and it doesn not find anything.
0
 
for_yanCommented:
So you have a list and you want to find those in the list which are permiutatios of the other?
Correct?

If correct, then send me the list,
if not correct, then explain again.
0
 
robo_docAuthor Commented:
I want to find out if the permutations generated using the code above are present in the word list
0
 
for_yanCommented:


I guees  this is what you want:

import java.util.ArrayList;
import java.util.List;


public class Permutations {
    public static void main(String [] args){

        new Permutations();
       // System.out.println(permute("1234"));

    }

    public Permutations(){
        ArrayList given = new ArrayList();
        String myWord = "love";
        given.add("evol");
        given.add("levo");
        given.add("123");

        List res =   permute(myWord);

        for(int j=0; j<given.size(); j++){
            String s = (String)given.get(j);
            if(res.contains(s))System.out.println(s);
        }



    }

  public static  List<String> permute(String s) {
        List<String> permutations = new ArrayList<String>();
        if (s.length() == 0) {
            permutations.add("");
            return permutations;
        }
        char firstChar = s.charAt(0);
        String restOfString = s.substring(1);
        List<String> words = permute(restOfString);
        for (String word : words) {
            for (int i = 0; i <= word.length(); i++)
                permutations.add(insertCharAt(word, firstChar, i));
        }
        return permutations;
    }

   static String insertCharAt(String word, char c, int i) {
        String start = word.substring(0, i);
        String end = word.substring(i);
        return start + c + end;
    }

}

Open in new window


Output:

evol
levo

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now