其他分享
首页 > 其他分享> > leetcode 每日一题 953. 验证外星语词典

leetcode 每日一题 953. 验证外星语词典

作者:互联网

leetcode 每日一题 953. 验证外星语词典

class Solution {
    public boolean isAlienSorted(String[] words, String order) {
        char[] chars = order.toCharArray();
        int[] arr = new int[chars.length];
        for (int i = 0; i < chars.length; i++) {
            arr[chars[i] - 'a'] = i;
        }

        for (int i = 1; i < words.length; i++) {
            char[] a = words[i - 1].toCharArray();
            char[] b = words[i].toCharArray();
            for (int j = 0; j < a.length; j++) {
                if(j >= b.length){
                    return false;
                }
                 if(arr[a[j]-'a'] > arr[b[j]-'a']){
                    return false;
                }
                if(arr[a[j]-'a'] < arr[b[j]-'a']){
                   break;
                }
            }
        }
        return true;
    }
}

 

标签:char,arr,int,953,chars,length,words,leetcode,外星
来源: https://www.cnblogs.com/yexuba/p/16279682.html