Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
2
Indexable
Never
class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        unordered_map<char,int>m;
        int i=1;
        int n=order.length();
        for(auto position:order){
            m[position]=n;
            n--;
        }
        for(int x=0;x<words.size()-1;x++){
            //if(words[x].length()<=words[x+1].length()){
                for(int i=0;i<words.size();i++){
                    for(int j=0;j<words[x].length();j++){
                        if(words[i][j]==words[i+1][j]){
                            if(words[i]==words[i+1]){
                                return true;
                            }
                            continue;
                        }
                        else if(m[words[i][j]]<m[words[i+1][j]]){
                            return false;
                        }
                        else if(j==words[x+1].length()){
                            return false;
                        }
                        else{
                            return true;
                        }
                    }
                }
            //}
        }
        return false;
    }
};