gpt4 book ai didi

java - 如何在 Java 中对单词进行所有可能的转换

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:01 24 4
gpt4 key购买 nike

我目前正在开发一个程序,该程序将尝试根据单词中的字符对单词进行所有可能的转换。例如,如果单词是 hello,那么程序将打印出所有不同的变体,例如 hello、Hello、helloO、HelloO、h3ll0、H3lL0 等等,直到完成每个组合。这是我到目前为止制作的程序:

import java.io.*;

public class Transformer{
String input = null;

public Transformer(){
run();
}

public void run(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter a word:");

try {
input = br.readLine();
} catch (IOException e) {
System.out.println("Error");
System.exit(1);
}

//Convert String into char array
char charArray[] = input.toCharArray();

//Capitalise first letter
if(Character.isLetter(charArray[0])){
charArray[0] = Character.toUpperCase(charArray[0]);
}
System.out.println(charArray);

//Reset transformation
charArray = input.toCharArray();

//Capitalise last letter
if(Character.isLetter(charArray[charArray.length - 1])){
charArray[charArray.length - 1] = Character.toUpperCase(charArray[charArray.length - 1]);
}
System.out.println(charArray);

//Reset transformation
charArray = input.toCharArray();


//Iterate through charArray to make transformations to characters
for(int i = 0; i < charArray.length; i++){
if(charArray[i] == 'a'){
charArray[i] = '@';
System.out.println(charArray);
}

if(charArray[i] == 'o'){
charArray[i] = '0';
System.out.println(charArray);
}

if(charArray[i] == 'e'){
charArray[i] = '3';
System.out.println(charArray);
}

if(charArray[i] == 's'){
charArray[i] = '5';
System.out.println(charArray);
}

if(charArray[i] == 'i'){
charArray[i] = '1';
System.out.println(charArray);
}

if(charArray[i] == 'l'){
charArray[i] = '7';
System.out.println(charArray);
}
}
}
}

问题是,这只会创建有限数量的转换并将它们相继添加,而不是一次进行一个转换,然后进行两个,依此类推。我能想到的唯一解决方案是永无止境的 if 语句列表,但即便如此,我也不确定如何跟踪更改。实现这一目标的最佳方法是什么?

最佳答案

我告诉过你这样的事情:

public class WordTransformer {

private HashMap<Character, char[]> transformMap = new HashMap<Character, char[]>();
{
transformMap.put('h', new char[]{'H', 'h'});
transformMap.put('e', new char[]{'3', 'E'});
transformMap.put('l', new char[]{'I', 'L'});
transformMap.put('o', new char[]{'0', 'O'});
}

public List<String> doTransform(String s){
char[] stringChars = s.toCharArray();
List<String> versions = new ArrayList<String>();
doTransform(s, stringChars, 0, versions);
return versions;
}

//This method is recursive
private void doTransform(String s, char[] stringChars, int index,
List<String> versions){

//if we reached the end of string in current iteration
//add generated variant into list
if(index==s.length()){
versions.add(new String(stringChars));
return;
}

char c = s.charAt(index);
if(transformMap.containsKey(c)){
char[] transforms = transformMap.get(c);
for(char t : transforms){
stringChars[index] = t;
//And this is recursive invocation, that means method calls itself
doTransform(s, stringChars, index+1, versions);
}
}else {
//if there is no such character in transform map, process next index
doTransform(s, stringChars, index+1, versions);
}
}

}

请注意,私有(private)方法 doTransform 会自行调用。这称为递归方法。另请注意,我们将原始字符串和 chars 数组传递给此方法。 Array - 是一个地方,我们在这里构建原始字符串的每个版本。用法:

WordTransformer transformer = new WordTransformer();
List<String> list = transformer.doTransform("hello");
for(String s : list){
System.out.println(s);
}

希望这会有所帮助。

关于java - 如何在 Java 中对单词进行所有可能的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com