Java混合字符串字,使用布尔值来检查

问题描述 投票:-3回答:1
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;                                        
  }                                     
}    

主要

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

我试图返回并输出如果一个或两个的值与三个相同,它将返回true,否则为false。但是,我的输出总是正确的,它应该是true,false,true和false。是因为它只检查每个字符串中的相同字母,所以它返回true?

java
1个回答
1
投票

我对你的问题采取了不同的方法。有关详细信息,请参阅代码注释。

码:

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

结果:

true
false
true
false
© www.soinside.com 2019 - 2024. All rights reserved.