Sarin Vadakkey Thayyil
Sarin Vadakkey Thayyil

Reputation: 954

java program to find all dictionary words from a given string

I need a java program which should give all dictionary words from the give string combination. Example: If the String given is "IOSMNEAB", it is a string with length 8. I need words like IS, NAME, SOME, SON, MEAN etc..

My solution: I made all permutations of the string and searched in a already created dictionary database. But it only gives words with 8 digit length. I need words with length >=2 and <=8. Please Help :-(

   import java.sql.DriverManager;
   import java.sql.SQLException;
   import java.util.Random;
   import java.sql.*;
   public final class Combination1 {    

  static String url = "jdbc:mysql://localhost:3306/file";
  static int count;
  static String Vowel = "AEIOU";
  static String Consonant = "BCDFGHJKLMNPQRSTVWXYZ";
  static StringBuffer word;
  static Connection con;
  static ResultSet rs;
  static PreparedStatement preparedStatement;
  static Random randomGenerator = new Random();

   public static final void main(String... aArgs){
  String str="";
  for(int i=0;i<5;i++){
      char a = getRandomConsonant(); 
      str = str+a;}
  for(int i=0;i<3;i++){
      char a = getRandomVowel(); 
      str = str+a;}
    StringBuffer strBuf = new StringBuffer(str);
    count = 0;
    doPerm(strBuf,str.length());

    if(count>=1){
        try
          {
             Class.forName("com.mysql.jdbc.Driver");
             try
             {              
                con = DriverManager.getConnection(url,"root","1234");
                preparedStatement = con.prepareStatement("insert into FILE.WORDS values (?,?)");
                preparedStatement.setString(1, str);
                preparedStatement.setInt(2, count);
                preparedStatement.executeUpdate();
             }

             catch (SQLException ex)
             {
                ex.printStackTrace();
             }
          }
        catch(ClassNotFoundException e)
          {
             System.out.println(e);
          }
    }

      }

      private static char getRandomVowel(){
  int randomInt = randomGenerator.nextInt(4);
  return Vowel.charAt(randomInt);
  }
      private static char getRandomConsonant(){
  int randomInt = randomGenerator.nextInt(20);
  return Consonant.charAt(randomInt);
  }
      private  static void swap(StringBuffer str, int pos1, int pos2){
    char t1 = str.charAt(pos1);
    str.setCharAt(pos1, str.charAt(pos2));
    str.setCharAt(pos2, t1);
} 
    private static void doPerm(StringBuffer str, int index){

    if(index == 0)
    {
     System.out.println(str);
     try
      {  
         String word = str.toString();
         Class.forName("com.mysql.jdbc.Driver");
         try
         {              
            con = DriverManager.getConnection(url,"root","1234");
            preparedStatement = con.prepareStatement("SELECT * FROM DICT WHERE word=?");
            preparedStatement.setString(1, word);
            rs = preparedStatement.executeQuery(); 
            rs = preparedStatement.getResultSet();
            if(rs.next()){
                count++;
            }
         }

         catch (SQLException ex)
         {
            ex.printStackTrace();
         }
      }
    catch(ClassNotFoundException e)
      {
         System.out.println(e);
      }
    }


    else {
        doPerm(str, index-1);
        int currPos = str.length()-index;
        for (int i = currPos+1; i < str.length(); i++) {
            swap(str,currPos, i);
            doPerm(str, index-1);
            swap(str,i, currPos);
        }
    }
}
   }

Upvotes: 1

Views: 7069

Answers (3)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382454

In your existing code, supposing it effectively finds words with 8 chars length, you just have to replace the test permuted.equals(dictionaryWord) by permuted.contains(dictionaryWord) : this will return true if a part of you permuted word is equal to the dictionary word.

Upvotes: 1

dharam
dharam

Reputation: 8106

I would suggest you to use all possible subsets of the given string to generate all possible words.

Refer to the post below: Java Code for permutations of a list of numbers

Upvotes: 0

Jon Taylor
Jon Taylor

Reputation: 7905

You could take each of your permutations and check substrings for words. You dont always have to use the full String length.

Upvotes: 0

Related Questions