如果字符串可以由字符中的字符组成,则字符串是好的。我想返回所有好的字符串的长度之和。
输入:words = [“ cat”,“ bt”,“ hat”,“ tree”],chars =“ atach”
输出:6
说明:可以形成的字符串是“ cat”和“ hat”,因此答案是3 + 3 = 6。
下面是我编写的代码。
class Solution
{
public int countCharacters(String[] words, String chars)
{
int k = 0,count=0;
for(int i = 0; i < words.length; i++)
{
char[] ch = words[i].toCharArray();
for(int j = 0; j < ch.length; j++)
{
if(chars.contains(ch[j]))
{
k++;
}
}
if(k == words[i].length())
{
count+= Math.max(count,k);
}
}
return count;
}
}
Output:
Line 13: error: incompatible types: char cannot be converted to CharSequence
if(chars.contains(ch[j]))
有人可以帮我吗?访问角色时我做错了什么?