其他分享
首页 > 其他分享> > LeetCode 383 —— 赎金信

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;
    }
}

标签:ransomNote,index,return,int,caps,magazine,383,赎金,LeetCode
来源: https://blog.csdn.net/aaqian1/article/details/113924308