Java 比较两个字符串,如果一个字符串包含另一个字符串的所有字符,则返回true

Java 比较两个字符串,如果一个字符串包含另一个字符串的所有字符,则返回true,java,string,compare,Java,String,Compare,我在练习lintcode并试图解决这个问题 比较两个字符串A和B,确定A是否包含B中的所有字符 字符串A和B中的字符都是大写字母 我编写了这段代码,它检查A中B的每个字符,并从A中删除找到的字符,但不知怎的,它没有通过输入测试 A=“ABCD”和B=“ACC”。当它应该给出false时,它给出的输出为true 我不理解代码中的问题 public class Solution { /** * @param A : A string includes Upper Case letters

我在练习lintcode并试图解决这个问题

比较两个字符串A和B,确定A是否包含B中的所有字符

字符串A和B中的字符都是大写字母

我编写了这段代码,它检查A中B的每个字符,并从A中删除找到的字符,但不知怎的,它没有通过输入测试 A=“ABCD”和B=“ACC”。当它应该给出false时,它给出的输出为true

我不理解代码中的问题

    public class Solution {
/**
 * @param A : A string includes Upper Case letters
 * @param B : A string includes Upper Case letter
 * @return :  if string A contains all of the characters in B return true else return false
 */
public boolean compareStrings(String A, String B) {
    // write your code here
    int aLen = A.length();
    int bLen = B.length();
    if (aLen == 0) {
        return bLen == 0;
    }
    for(int i = 0; i<bLen; i++){
        String temp = B.substring(i,i);
        if(A.contains(temp))
            A.replace(temp, "");
        else
            return false;
    }
    return true;
}
公共类解决方案{
/**
*@param A:字符串包含大写字母
*@param B:字符串包含大写字母
*@return:如果字符串A包含B中的所有字符,则返回true,否则返回false
*/
公共布尔比较器字符串(字符串A、字符串B){
//在这里编写代码
int aLen=A.length();
int bLen=B.长度();
如果(aLen==0){
返回bLen==0;
}

对于(int i=0;i您每次只希望从
A
中删除一个字符。否则,下次在
B
中遇到同一个字符时,该字符将不会出现

尝试使用
replaceFirst
而不是
replace

您还需要使用
A=A.replaceFirst(…)
将结果分配回
A
,因为否则您实际上不会更改
字符串

最后,
B.substring(i,i);
是长度为
0
的字符串,而不是长度为
1
的字符串。请尝试
“”+B.charAt(i)
B.substring(i,i+1)

这是一个完整的版本,可以工作

public static boolean compareStrings(String A, String B) {
    // write your code here
    int aLen = A.length();
    int bLen = B.length();
    if (aLen == 0) {
        return bLen == 0;
    }
    for(int i = 0; i<bLen; i++){
        String temp = B.substring(i,i + 1);
        if(A.contains(temp))
            A = A.replaceFirst(temp, "");
        else
            return false;
    }
    return true;
}
公共静态布尔比较器字符串(字符串A、字符串B){
//在这里编写代码
int aLen=A.length();
int bLen=B.长度();
如果(aLen==0){
返回bLen==0;
}

对于(int i=0;i您每次只希望从
A
中删除一个字符。否则,下次在
B
中遇到同一个字符时,该字符将不会出现

尝试使用
replaceFirst
而不是
replace

您还需要使用
A=A.replaceFirst(…)
将结果分配回
A
,因为否则您实际上不会更改
字符串

最后,
B.substring(i,i);
是长度为
0
的字符串,而不是长度为
1
的字符串。请尝试
“”+B.charAt(i)
B.substring(i,i+1)

这是一个完整的版本,可以工作

public static boolean compareStrings(String A, String B) {
    // write your code here
    int aLen = A.length();
    int bLen = B.length();
    if (aLen == 0) {
        return bLen == 0;
    }
    for(int i = 0; i<bLen; i++){
        String temp = B.substring(i,i + 1);
        if(A.contains(temp))
            A = A.replaceFirst(temp, "");
        else
            return false;
    }
    return true;
}
公共静态布尔比较器字符串(字符串A、字符串B){
//在这里编写代码
int aLen=A.length();
int bLen=B.长度();
如果(aLen==0){
返回bLen==0;
}
对于(int i=0;iString类的replace()方法将用第二个参数替换第一个参数的每次出现,请改用replaceFirst()尝试。示例实现

    public static boolean compareStrings(String A, String B) {
            boolean isOk = true;

            for (int i = 0;i < B.length();i++) {
                    if (!A.contains(B.charAt(i) + "")) {
                        isOk = false;
                        break;
                    }
                    A = A.replaceFirst(B.charAt(i) + "", ""); 
            }
            return isOk;
    }
公共静态布尔比较器字符串(字符串A、字符串B){
布尔值isOk=true;
对于(int i=0;i
String类的replace()方法将用第二个参数替换第一个参数的每次出现,请改用replaceFirst()尝试。示例实现

    public static boolean compareStrings(String A, String B) {
            boolean isOk = true;

            for (int i = 0;i < B.length();i++) {
                    if (!A.contains(B.charAt(i) + "")) {
                        isOk = false;
                        break;
                    }
                    A = A.replaceFirst(B.charAt(i) + "", ""); 
            }
            return isOk;
    }
公共静态布尔比较器字符串(字符串A、字符串B){
布尔值isOk=true;
对于(int i=0;i
字符串
A
AA
的结果应该是什么?主要问题是
A.replace(temp,“”;
应该是
A=A.replace(temp,“”)
。我同意Pshemo的说法,这个问题也可以更清楚。@PaulBoddington我试过了。还是没有working@Pshemo据我所知,对于输入A,AA,输出应该是错误提示:尝试打印
temp
。字符串
A
AA
的结果应该是什么?主要问题是
A(temp,“”;
应为
A=A.replace(temp,“”)
。我同意Pshemo的说法,这个问题也可以更清楚。@PaulBoddington我试过了。还是没有working@Pshemo据我所知,对于输入A,AA,输出应该是错误提示:尝试打印
temp
。我尝试了你的两个建议。没有任何效果。是的,我的原始代码是这个B.子字符串(I,I+1)但事实并非如此working@SadiqHusainKhan你能给我一个例子,说明这3个变化不起作用吗?B.子字符串(i,i+1)对输入不起作用:“A”,“error:index out of of”bounds@SadiqHusainKhan这是不正确的。如果
B
“”
然后
bLen
0
,因此循环甚至不会被输入。我想问题是你发布的代码不是你正在使用的实际版本。我尝试了你的两个建议。没有任何效果。是的,我的原始代码是这个B.子字符串(I,I+1)但事实并非如此working@SadiqHusainKhan你能给我一个例子,说明这3个变化不起作用吗?B.子字符串(i,i+1)对输入不起作用:“A”,“error:index out of of”bounds@SadiqHusainKhan这是不正确的。如果
B
“”
那么
bLen
0
,因此循环甚至不会被输入。我认为问题在于您发布的代码不是您正在使用的实际版本。感谢您的回复谢谢您的回复