【leetcode-字符计数】赎金信/字符串中的第一个唯一字符/找不同/最长回文串

赎金信

给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)

示例 1:
输入:ransomNote = “a”, magazine = “b”
输出:false

示例 2:
输入:ransomNote = “aa”, magazine = “ab”
输出:false

示例 3:
输入:ransomNote = “aa”, magazine = “aab”
输出:true

计数

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] count = new int[26];
        char[] rChars = ransomNote.toCharArray(), mChars = magazine.toCharArray();
        for (char m : mChars)
            count[m - 'a']++;
        for (char r : rChars)
            if (count[r - 'a']-- <= 0)
                return false;
        return true;
    }
}

字符串中的第一个唯一字符

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

示例:
s = “leetcode”
返回 0
s = “loveleetcode”
返回 2

计数

class Solution {
    public int firstUniqChar(String s) {
        int[] count = new int[26];
        char[] sChars = s.toCharArray();
        for (char c : sChars)
            count[c - 'a']++;
        for (int i = 0; i < sChars.length; i++)
            if (count[sChars[i] - 'a'] == 1)
                return i;
        return -1;
    }
}

indexOf

class Solution {
    public int firstUniqChar(String s) {
        int min = s.length();
        for (char c = 'a'; c <= 'z'; c++) {
            int start = s.indexOf(c);
            int end = s.lastIndexOf(c);
            if (start != -1 && start == end)
                min = Math.min(start, min);
        }
        return min == s.length() ? -1 : min;
    }
}

找不同

给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。

示例 1:
输入:s = “abcd”, t = “abcde”
输出:“e”
解释:‘e’ 是那个被添加的字母。

示例 2:
输入:s = “”, t = “y”
输出:“y”

示例 3:
输入:s = “a”, t = “aa”
输出:“a”

示例 4:
输入:s = “ae”, t = “aea”
输出:“a”

计数

class Solution {
    public char findTheDifference(String s, String t) {
        int[] count = new int[26];
        char[] sChars = s.toCharArray(), tChars = t.toCharArray();
        for (char c : sChars)
            count[c - 'a']++;
        for (char c : tChars)
            if (count[c - 'a']-- == 0)
                return c;
        return 'a';
    }
}

求和

class Solution {
    public char findTheDifference(String s, String t) {
        int sum = 0;
        char[] sChars = s.toCharArray(), tChars = t.toCharArray();
        for (char c : tChars)
            sum += c;
        for (char c : sChars)
            sum -= c;
        return (char)sum;
    }
}

位运算

class Solution {
    public char findTheDifference(String s, String t) {
        int result = 0;
        char[] sChars = s.toCharArray(), tChars = t.toCharArray();
        for (char c : tChars)
            result ^= c;
        for (char c : sChars)
            result ^= c;
        return (char)result;
    }
}

最长回文串

给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。
在构造过程中,请注意区分大小写。比如 “Aa” 不能当做一个回文字符串。

注意:
假设字符串的长度不会超过 1010。

示例 1:
输入:
“abccccdd”
输出:
7
解释:
我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。

字符计数

class Solution {
    public int longestPalindrome(String s) {
        int[] count = new int[60];
        char[] sChars = s.toCharArray();
        int sum = 0;
        for (char c : sChars) {
            if ((++count[c - 'A'] & 1) == 0) 
                sum += 2;
        }
        if (sum < s.length())
            sum++;
        return sum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值