class Solution {
public int maxLength(List<String> arr) {
List<Integer> res=new ArrayList<>();
res.add(0);
int ans=0;
for(int i=0;i<arr.size();i++){
String s=arr.get(i);
int cur=0;
for(int j=0;j<s.length();j++){
int ch=s.charAt(j)-'a';
if((cur&1<<ch)==0)
cur|=1<<ch;
else{
cur=0;
break;
}
}
if(cur==0)continue;
int len=res.size();
for(int j=0;j<len;j++){
if((cur & res.get(j))==0){
res.add(cur|res.get(j));
ans=Math.max(ans,Integer.bitCount(cur|res.get(j)));
}
}
}
return ans;
}
}
串联字符串的最大长度(位运算)
最新推荐文章于 2022-11-10 19:01:32 发布
这篇博客详细介绍了如何使用Solution类中的方法,通过位操作技巧求解给定字符串数组中,每个字符串由不同字符组成且相邻字符间满足特定位运算条件的最长子集长度。作者展示了如何遍历数组,利用位掩码和集合操作找到并更新最大子集计数。
摘要由CSDN通过智能技术生成