java程序从给定的字符串中查找所有字典单词

时间:2012-07-16 13:15:48

标签: java string word

我需要一个java程序,它应该给出来自给定字符串组合的所有字典单词。 示例:如果给定的字符串是" IOSMNEAB",则它是长度为8的字符串。 我需要像IS,NAME,SOME,SON,MEAN等字样。

我的解决方案:我对字符串进行了所有排列,并在已创建的字典数据库中进行了搜索。 但它只提供8位数字的单词。我需要长度为> = 2且< = 8的单词。 请帮助: - (

   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);
        }
    }
}
   }

3 个答案:

答案 0 :(得分:1)

在您现有的代码中,假设它有效地找到了8个字符长度的单词,您只需要将permuted.equals(dictionaryWord)替换为permuted.contains(dictionaryWord):如果您的一部分置换单词相等,则返回true字典词。

答案 1 :(得分:0)

您可以对每个排列进行检查并检查字符串的字符串。你不必总是使用完整的字符串长度。

答案 2 :(得分:0)

我建议你使用给定字符串的所有可能子集来生成所有可能的单词。

请参阅下面的帖子: Java Code for permutations of a list of numbers