1. 程式人生 > 實用技巧 >LeetCode 字串

LeetCode 字串

基礎部分

242. 有效的字母異位詞

簡單

給定兩個字串 st ,編寫一個函式來判斷 t 是否是 s 的字母異位詞。

示例 1:

輸入: s = "anagram", t = "nagaram"
輸出: true

示例 2:

輸入: s = "rat", t = "car"
輸出: false

說明:
你可以假設字串只包含小寫字母。

進階:
如果輸入字串包含 unicode 字元怎麼辦?你能否調整你的解法來應對這種情況?

class Solution {
    public boolean isAnagram(String s, String t) {
        int[] map = new int[26];
        for (char c : s.toCharArray()) map[c-'a']++;
        for (char c : t.toCharArray()) map[c-'a']--;
        for (int count : map) if (count != 0) return false;
        return true;
    }
}

409. 最長迴文串

簡單

給定一個包含大寫字母和小寫字母的字串,找到通過這些字母構造成的最長的迴文串。

在構造過程中,請注意區分大小寫。比如 "Aa" 不能當做一個迴文字串。

注意:
假設字串的長度不會超過 1010。

示例 1:

輸入:
"abccccdd"

輸出:
7

解釋:
我們可以構造的最長的迴文串是"dccaccd", 它的長度是 7。
class Solution {
    public int longestPalindrome(String s) {
        int[] letters = new int[52];
        for (char c : s.toCharArray()){
            if (c <= 'Z') letters[c-'A']++;
            else letters[c-'a'+26]++;
        }
        int res = 0, odd = 0;
        for (int num : letters){
            if (num % 2 == 1) odd = 1;
            res += num / 2 * 2;
        }
        return res + odd;
    }
}

205. 同構字串

簡單

給定兩個字串 s 和 *t*,判斷它們是否是同構的。

如果 s 中的字元可以被替換得到 *t* ,那麼這兩個字串是同構的。

所有出現的字元都必須用另一個字元替換,同時保留字元的順序。兩個字元不能對映到同一個字元上,但字元可以對映自己本身。

示例 1:

輸入: s = "egg", t = "add"
輸出: true

示例 2:

輸入: s = "foo", t = "bar"
輸出: false

示例 3:

輸入: s = "paper", t = "title"
輸出: true

說明:
你可以假設 st 具有相同的長度。

class Solution {
    public boolean isIsomorphic(String s, String t) {
        int len = s.length();
        Map<Character,Character> map1 = new HashMap<>();
        Map<Character,Character> map2 = new HashMap<>();
        for (int i = 0; i < len; i++){
            char ch1 = s.charAt(i);
            char ch2 = t.charAt(i);
            if (map1.containsKey(ch1)){
                if (map1.get(ch1) != ch2) return false;
            }else if (map2.containsKey(ch2)){
                if (map2.get(ch2) != ch1) return false;
            }else {
                map1.put(ch1, ch2);
                map2.put(ch2, ch1);
            }
        }
        return true;
    }
}

647. 迴文子串

中等

給定一個字串,你的任務是計算這個字串中有多少個迴文子串。

具有不同開始位置或結束位置的子串,即使是由相同的字元組成,也會被計為是不同的子串。

示例 1:

輸入: "abc"
輸出: 3
解釋: 三個迴文子串: "a", "b", "c".

示例 2:

輸入: "aaa"
輸出: 6
說明: 6個迴文子串: "a", "a", "a", "aa", "aa", "aaa".

注意:

  1. 輸入的字串長度不會超過1000。
class Solution { //O(n^3)
    public int countSubstrings(String s) {
        int len = s.length();
        int res = 0;
        for (int i = 0; i < len; i++){
            for (int j = i+1; j <= len; j++){
                if (is(s.substring(i, j))) res++;
            }
        }
        return res;
    }
    
    private boolean is(String s){
        int l = s.length();
        for (int i = 0; i < l/2; i++){
            if (s.charAt(i) != s.charAt(l-i-1))
                return false;
        }
        return true;
    }
}
class Solution { //O(n^2),從裡向外的雙指標
    int res = 0;
    public int countSubstrings(String s) {
        if (s == null || s.length() == 0) return 0;
        int len = s.length();
        for (int i = 0; i < len; i++){
            helper(s, i, i, len);
            helper(s, i, i+1, len);
        }
        return res;
    }
    
    private void helper(String s, int l, int r, int len){
        while (l >= 0 && r < len && s.charAt(l) == s.charAt(r)){
            res++;
            l--;
            r++;
        }
    }
}

9. 迴文數

簡單

判斷一個整數是否是迴文數。迴文數是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數。

示例 1:

輸入: 121
輸出: true

示例 2:

輸入: -121
輸出: false
解釋: 從左向右讀, 為 -121 。 從右向左讀, 為 121- 。因此它不是一個迴文數。

示例 3:

輸入: 10
輸出: false
解釋: 從右向左讀, 為 01 。因此它不是一個迴文數。
class Solution {
    public boolean isPalindrome(int x) {
        String s = String.valueOf(x);
        for (int i = 0; i < s.length()/2; i++){
            if (s.charAt(i) != s.charAt(s.length()-i-1))
                return false;
        }
        return true;
    }
}

696. 計數二進位制子串

簡單

給定一個字串 s,計算具有相同數量0和1的非空(連續)子字串的數量,並且這些子字串中的所有0和所有1都是組合在一起的。

重複出現的子串要計算它們出現的次數。

示例 1 :

輸入: "00110011"
輸出: 6
解釋: 有6個子串具有相同數量的連續1和0:“0011”,“01”,“1100”,“10”,“0011” 和 “01”。

請注意,一些重複出現的子串要計算它們出現的次數。

另外,“00110011”不是有效的子串,因為所有的0(和1)沒有組合在一起。

示例 2 :

輸入: "10101"
輸出: 4
解釋: 有4個子串:“10”,“01”,“10”,“01”,它們具有相同數量的連續1和0。

注意:

  • s.length 在1到50,000之間。
  • s 只包含“0”或“1”字元。
class Solution {
    int res = 0;
    public int countBinarySubstrings(String s) {
        if (s == null || s.length() == 0) return 0;
        char[] chars = s.toCharArray();
        int len = chars.length;
        for (int i = 0; i < len-1; i++){
            if (chars[i] != chars[i+1])
                helper(chars, i, i+1, len); 
        }
        return res;
    }
    
    private void helper(char[] chars, int l, int r, int len){
        char left = chars[l];
        char right = chars[r];
        do {
            res++;
            l--;
            r++; 
        }while (l >= 0 && r < len && chars[l] == left && chars[r] == right);
    }
}

頻率排序

72,5,3,93,468,415,583,556,555,165,214,385,833,536,10,1106,1096,761,564,271,159,722,336,539,97,443,91,758,76,22,227