【发布时间】:2015-11-08 03:46:52
【问题描述】:
我正在练习 lintcode 并试图解决这个问题
比较两个字符串A和B,判断A是否包含B中的所有字符。
字符串 A 和 B 中的字符都是大写字母。
我编写了这段代码,它检查 A 中 B 的每个字符并从 A 中消除找到的字符,但不知何故它未能通过输入测试 A = "ABCD" 和 B = "ACC" 。当它应该给出假时,它给出的输出为真
我不明白我的代码中的问题
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;
}
}
【问题讨论】:
-
字符串
A和AA的结果应该是什么? -
主要问题是
A.replace(temp, "");应该是A = A.replace(temp, "");。我同意 Pshemo,问题也可以更清楚。 -
@PaulBoddington 我试过了。还是不行
-
@Pshemo 据我了解的问题,对于输入 A,AA,输出应该是 false
-
提示:尝试打印
temp。