如何正确排序字符串的字符以与Java中的另一个字符串进行比较?

问题描述:

我对Java很新,我目前正在研究一个能够从用户输入2个字符串并查看第二个字符串是否包含在第一个字符串中的“隐藏字”程序。我一直在处理的棘手部分是第一个字符串中的单词不必与第二个字符串的顺序相同。如何正确排序字符串的字符以与Java中的另一个字符串进行比较?

例如,单词“TOT”可以在单词“番茄”,即使它不存在在确切顺序找到。

我想通了,我可以在字符串中的字符进行排序,以测试他们是否能匹配,但每当我尝试使用测试数据,它总是打印出的文字无法从1弦被发现。

如果任何人都可以给我一个提示,我很想念我会很感激。我真的不明白为什么它总是打印出不是。

作为另一个说明,如果您想使用不同长度的字符串,我会在BitSet util的某个位置读取比字符数组更好的选项,但我不确定这是否为真,或者它甚至将它排序字符。

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

您目前的做法是行不通的,因为你检查,看看是否两个bitset代表您输入的字符串是相等的,他们不会,除非这两个字符串具有完全相同的字母。我不认为排序字符串中的字母也可以。即使对两个字符串进行排序,也不会在序列“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; 
} 
+0

感谢您的帮助。我测试了代码,它适用于连续的字符。当我使用上面提到的测试数据时,单词“tot”在单词“tomato”中仍然无法识别,我会尝试着解决这个问题。感谢您的帮助,如果您有任何想法或资源我可以查看,以便我可以解决这个问题,我会很感激。 – Albert 2014-12-04 21:13:44

最佳的解决方案是,如果计数的存在是为了保持整数的数组,其存储每个字符的计数在主string.Then检查在测试串中的每个字符在countArr.If计数approches 0突破循环。此解决方案将复杂性优化为O(n),而不是使用O(n^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; 
     } 
    }