[英]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. 我需要一个Java程序,该程序应从给定字符串组合中给出所有字典单词。 Example: If the String given is "IOSMNEAB", it is a string with length 8. I need words like IS, NAME, SOME, SON, MEAN etc.. 示例:如果给定的字符串是“ IOSMNEAB”,则它是一个长度为8的字符串。我需要像IS,NAME,SOME,SON,MEAN等单词。
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. 但是它只能给出8位数字的单词。 I need words with length >=2 and <=8. 我需要长度> = 2和<= 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);
}
}
}
}
在您现有的代码中,假设它可以有效地找到8个字符的单词,则只需将测试permuted.equals(dictionaryWord)
替换为permuted.contains(dictionaryWord)
:如果一部分置换单词等于字典中的单词。
You could take each of your permutations and check substrings for words. 您可以获取每个排列并检查子字符串中的单词。 You dont always have to use the full String length. 您不必总是使用完整的String长度。
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 请参阅以下文章: 用于数字列表排列的Java代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.