简体   繁体   English

Java混合字符串词,使用boolean进行检查

[英]Java mixed string words, using boolean to check

public class Test{                                      
  public static boolean s(String one, String two, String three){                                        
    int[] array = new int[30];                                      
    for (char c : three.toCharArray()) {                                        
      array[c - 97]++;                                      
    }                                       
    for (char c : two.toCharArray()){                                       
      array[c - 97]--;                                      
    }                                       
    for(char c : one.toCharArray()){                                        
      array[c - 97]--;                                      
    }                                       
    for (int value : array){                                        
      if (value > 0){                                       
        return false;                                       
      }                                     
    }                                       
    return true;                                        
  }                                     
}    

Main主要的

public class Main                                       
{                                       
  public static void main(String[] args)                                        
  {                                                                             
    System.out.println(Test.s("test","now","tesntow"));       //true                                                                     
    System.out.println(Test.s("test","here","testhere"));    //false  
    System.out.println(Test.s("test","here","tehstere"));          //true                                                                
    System.out.println(Test.s("test","new","tesntewword"));      //false                                                                                     
  }                                     
}

I am trying to return and output where if one or two's value is the same as three, it would return true, else false.我正在尝试返回并输出,如果一或二的值与三相同,则返回 true,否则返回 false。 However, my output is always true, when it should be true, false, true and false.但是,我的输出始终为真,应该是真、假、真和假。 Is it because it is only checking for same letters in each string so it returns true?是不是因为它只检查每个字符串中的相同字母,所以它返回 true?

I have made a different approach to your problem.我对你的问题采取了不同的方法。 Refer to the code comments for details.有关详细信息,请参阅代码注释。

Code:代码:

public class Main2 {

    static int counter = 0;

    public static void main(String[] args) {
        System.out.println(s("test", "now", "tesntow")); // true
        System.out.println(s("test", "here", "testhere")); // false
        System.out.println(s("test", "here", "tehstere")); // true
        System.out.println(s("test", "new", "tesntewword")); // false
    }

    public static boolean s(String one, String two, String three) {
        String combined = one + two; // combine the two words
        combined = combined.toLowerCase(); // convert to lowercase
        three = three.toLowerCase(); // convert to lowercase

        // check if it has the same length, if not then return false
        // check if it is the same, if it is then return false
        if (combined.length() != three.length() || combined.equals(three)) {
            return false;
        }

        // iterate through the combined words
        for (int i = 0; i < combined.length(); i++) {
            // declare and initialize a flag
            boolean flag = false;

            // check if the current letter of the combine word is in the third word
            flag = three.contains(combined.charAt(i) + ""); // just add empty string to make it a string (Brute force)
            if (!flag) {
                return false;
            }

            // remove the letter from the third word
            int j = three.indexOf(combined.charAt(i));
            three = three.substring(0, j) + three.substring(j);
        }
        return true;
    }
}

Result:结果:

true
false
true
false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM