LeetCode 383 —— 赎金信

https://leetcode-cn.com/problems/ransom-note/solution/java-1ms-da-lao-xie-fa-fei-yuan-chuang-dai-ma-by-f/

很奇妙的想法,膜拜大佬:

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if(magazine.length() < ransomNote.length()) return false;
        int[] caps = new int[26];
        char[] charArray = ransomNote.toCharArray();
        for(char c:charArray){
            int index = magazine.indexOf(c, caps[c-'a']);
            if(index < 0) return false;
            caps[c-'a'] = index+1; 
        }
        return true;
    }
}
上一篇:【哈希表】leetcode383——赎金信


下一篇:leetcode 0383 就赎金 python与c++