如何正确地对字符串的字符进行排序,以便与Java中的另一个字符串进行比较?
我对Java非常陌生,目前正在开发一个隐藏字程序,可以从用户那里获取输入,2个字符串,并查看第2个字符串是否包含在第1个字符串中。我一直在处理的棘手部分是,第一个字符串中的单词不必与第二个字符串的顺序相同 例如,tot这个词可以在单词tomato中找到,尽管它不是按那个确切的顺序出现的 我发现我可以对字符串中的字符进行排序,以测试它们是否匹配,但每当我尝试使用测试数据时,它总是打印出无法从第一个字符串中找到单词 如果有人能给我一个暗示,告诉我我错过了什么,我将非常感激。我真的不明白为什么它总是打印成“否” 另一个注意事项是,我在某个地方读到,如果你想使用不同长度的字符串,那么比特集util是一个比字符数组更好的选项,但我不确定这是真的还是它甚至对字符进行排序如何正确地对字符串的字符进行排序,以便与Java中的另一个字符串进行比较?,java,arrays,string,sorting,character,Java,Arrays,String,Sorting,Character,我对Java非常陌生,目前正在开发一个隐藏字程序,可以从用户那里获取输入,2个字符串,并查看第2个字符串是否包含在第1个字符串中。我一直在处理的棘手部分是,第一个字符串中的单词不必与第二个字符串的顺序相同 例如,tot这个词可以在单词tomato中找到,尽管它不是按那个确切的顺序出现的 我发现我可以对字符串中的字符进行排序,以测试它们是否匹配,但每当我尝试使用测试数据时,它总是打印出无法从第一个字符串中找到单词 如果有人能给我一个暗示,告诉我我错过了什么,我将非常感激。我真的不明白为什么它总是打
public static void main(String[] args)
{
input = new Scanner(System.in);
System.out.println("Please enter a word"); //prompts user for a word
String word = input.next();
System.out.println("Please enter a word you would like to search"); //prompts user again to enter a word that they would like to search for within the first word
String search = input.next();
if (usedChar(word).equals(usedChar(search))) //method call using the two input variables
{ //the if statement checks to see if the two Strings are equal
System.out.print("The word " + search + " is found in the word " + word);
}
else
{
System.out.print("The word was not found in " + word); //returns second print statement if the Strings do not match
}
}
public static BitSet usedChar(String s){//method to iterate through Strings
BitSet bs = new BitSet();
for (int i = 0; i < s.length(); i++) {
bs.set(s.charAt(i));
}
return bs;
}
您当前的方法不起作用,因为您正在检查表示输入字符串的两个位集是否相等,除非这两个字符串具有完全相同的字母,否则它们不会相等。我认为在字符串中对字母进行排序也行不通。即使两个字符串都已排序,也无法在序列aabbcc中找到序列abc
更好的方法可能是从每个字符串创建一个HashMap,每个字母作为键,它出现的次数作为值。然后检查第一个单词,确保每个字母的出现次数足以隐藏第二个单词。性能可以提高,但您是否可以尝试以下代码
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a word"); // prompts user for a word
String word = input.next();
char[] charOfWrds = word.toCharArray();
System.out.println("Please enter a word you would like to search");
String search = input.next();
char[] charOfSrch = search.toCharArray();
if (isContains(charOfWrds, charOfSrch)) // method call using the
// two input variables
{ // the if statement checks to see if the two Strings are equal
System.out.print("The word " + search + " is found in the word "
+ word);
} else {
System.out.print("The word was not found in " + word);
}
}
public static Boolean isContains(char[] charOfWrds, char[] charOfSrch) {
int count = 0;
for (char cha : charOfSrch) {
for (char chaaa : charOfSrch) {
if (cha == chaaa)
count++;
}
}
if (count == charOfSrch.length)
return true;
return false;
}
最佳解决方案是维护一个int数组,该数组存储主字符串中每个字符的计数。然后检查测试字符串中的每个字符,如果countArr中存在计数。如果计数接近0,则突破循环。此解决方案将复杂性优化为开,而不是使用带开^2的嵌套for循环 方法countChar统计主字符串中每个字符的出现次数,方法checkChar检查测试字符串中的每个字符是否有足够的出现次数
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a word"); // prompts user for a word
String word = input.next();
int arr[] = countChar(word);
System.out.println("Please enter a word you would like to search");
String search = input.next();
if(checkChar(search, arr)==true){
System.out.println("Word Found!!");
}else{
System.out.println("Word cannot be found!!");
}
}
public static int[] countChar(String s) {
int[] countArr = new int[256];
for (int i = 0; i < s.length(); i++) {
countArr[s.charAt(i)] = countArr[s.charAt(i)] + 1;
}
return countArr;
}
public static boolean checkChar(String s, int[] countArr) {
for (int i = 0; i < s.length(); i++) {
if (countArr[s.charAt(i)] == 0) {
return false;
} else {
countArr[s.charAt(i)] = countArr[s.charAt(i)] - 1;
}
}
return true;
}
}
谢谢你的帮助。我测试了代码,它适用于连续的字符。当我使用上面提到的测试数据时,单词tot仍然无法在单词tomato中识别出来,我将尝试对此进行研究以解决此问题。感谢您的帮助,如果您有任何想法或资源,我可以研究,以便我可以解决这个问题,我将不胜感激。