LeetCode_Study_Plan/Programming Skills

953. Verifying an Alien Dictionary java

개발하는루루 2022. 9. 22. 22:24

https://leetcode.com/problems/verifying-an-alien-dictionary/?envType=study-plan&id=programming-skills-i 

 

Verifying an Alien Dictionary - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
    public boolean isAlienSorted(String[] words, String order){
        HashMap<String, Integer> map = new HashMap<>();

        for(int i = 0; i < order.length(); i++){
            map.put(String.valueOf(order.charAt(i)), i);
        }

        for(int i = 0; i < words.length - 1; i++){
            for(int j = 0; j < words[i].length(); j++){
                if (j >= words[i+1].length()) {
                    return false;
                }
                if (map.get(String.valueOf(words[i].charAt(j))) == map.get(String.valueOf(words[i+1].charAt(j)))) continue;
                if (map.get(String.valueOf(words[i].charAt(j))) > map.get(String.valueOf(words[i+1].charAt(j)))){
                    return false;
                }
                break;
            }
        }
        return true;
    }
    
}