leetcode 1684.统计一致字符串的数目(java)

在这里插入图片描述
关键:怎么判断一致性?
解决:1.用最大长度为26的(因为最多有26种英文字母)“0、1”组成的序列统计字符串中拥有的字符(拥有则在对应位置置1)。
2.判断一致性:两个字符串对应的“0,1”序列,进行相与操作···

扫盲:
1.String.charAt(int index)
返回位于字符串的指定索引处的字符。(字符串的索引从零开始)
eg:String s = “hello world”;
char result = s.charAt(6);
//则result为w

2.Y |= (1<<X)
把1左移X位,并且和Y进行或运算。即,把Y的第X位置1。

3.两个字符相减,即它们的ASCLL码值相减。(ASCLL码值直接的差距)
eg: int x = ‘b’ - ‘a’;
//a对应ASCLL码值:97
//b对应ASCLL码值:98
//则x的值为1

class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        int ref = trans(allowed);   
        int total = 0;   //用来统计“一致字符串”的个数
        for (String word : words) {
            int test = trans(word);
            if((test & ref) == test){
                total ++;
            }
        }
        return total;
    }

    public int trans(String s) {
        int ref = 0;
        for(int i = 0; i < s.length(); i++) {
            int x = s.charAt(i) - 'a';
            ref |= (0 << x);
        }
        return ref;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值