Verify Lexicographic Word Order under a Supplied Alphabet

Quick Overview

Verify Lexicographic Word Order under a Supplied Alphabet. The string order is a permutation of the lowercase English alphabet from earliest to latest.

Verify Lexicographic Word Order under a Supplied Alphabet

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem The string `order` is a permutation of the lowercase English alphabet from earliest to latest. Return whether `words` is lexicographically nondecreasing under that alphabet. At the first differing character use the supplied ranks; if one word is a prefix of another, the shorter word comes first. ```cpp class Solution { public: bool isAlienSorted(vector<string>& words, string order); }; ``` ### Examples **Example 1:** ```text Input: words = ["zoo","yak"], order = "zyxwvutsrqponmlkjihgfedcba" Output: true Explanation: Under this reversed alphabet,z precedes y. ``` **Example 2:** ```text Input: words = ["tap","ta"], order = "zyxwvutsrqponmlkjihgfedcba" Output: false Explanation: The second word is a proper prefix of the first, so it must come earlier regardless of letter ranks. ``` ### Constraints - `1 <= words.length <= 100`. - `1 <= words[i].length <= 20`. - order has length26 and is a permutation of all lowercase English letters. - Every word character is a lowercase English letter. ### Contract considerations The source supplies the alphabet permutation; the task does not ask you to infer an alphabet from words. ```hint Read the boundary condition Only the first nonmatching character of an adjacent word pair determines their relative order. ``` ### Boundary reminders The source supplies the alphabet permutation; the task does not ask you to infer an alphabet from words.

Overview: Verify Lexicographic Word Order under a Supplied Alphabet. The string order is a permutation of the lowercase English alphabet from earliest to latest.

Amazon logo
Amazon
Jun 22, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

The string order is a permutation of the lowercase English alphabet from earliest to latest. Return whether words is lexicographically nondecreasing under that alphabet. At the first differing character use the supplied ranks; if one word is a prefix of another, the shorter word comes first.

class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order);
};

Examples

Example 1:

Input: words = ["zoo","yak"], order = "zyxwvutsrqponmlkjihgfedcba"
Output: true
Explanation: Under this reversed alphabet,z precedes y.

Example 2:

Input: words = ["tap","ta"], order = "zyxwvutsrqponmlkjihgfedcba"
Output: false
Explanation: The second word is a proper prefix of the first, so it must come earlier regardless of letter ranks.

Constraints

  • 1 <= words.length <= 100 .
  • 1 <= words[i].length <= 20 .
  • order has length26 and is a permutation of all lowercase English letters.
  • Every word character is a lowercase English letter.

Contract considerations

The source supplies the alphabet permutation; the task does not ask you to infer an alphabet from words.

Boundary reminders

The source supplies the alphabet permutation; the task does not ask you to infer an alphabet from words.