---
# System prepended metadata

title: FB week2

---

# FB week2

```
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.

Example 1:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.

Example 2:
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
 
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
All characters in words[i] and order are English lowercase letters.
```

a-z
[1, 2, 3, 4, 5, 6, 7....,26]
abcde

[2, 5, 8, 4, 6]
adbbe

abd, adb


```
class Solution {
    int[] alphabet = new int[26];

    public boolean isAlienSorted(String[] words, String order) {
        for (int i = 0; i < order.length(); i++) {
            alphabet[order.charAt(i) - 'a'] = i;
        }
       
       for(int i = 1; i < words.length; i++) {
           int cmp = compare(words[i - 1], words[i]);
           if (cmp > 0) return false;
       }
       
        return true;
    }
    
    private int compare(String s1, String s2) {
        int n = s1.length();
        int m = s2.length();
        int cmp = 0;
        for (int i = 0, j = 0; i < n && j < m && cmp = 0; i++, j++) {
            cmp = alphabet[s1.charAt(i) - 'a'] - alphabet[s2.charAt(j) - 'a'];
        }
        
        return cmp == 0 ? m - n : cmp;
    }
}
```

```
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place and use only constant extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

3, 1, 2 -> 3, 2, 1

1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 1, 2
3, 2, 1

 0, 1, 2, 3, 4 -> 
[3, 5, 4, 2, 1] -> 4, 5, 3, 2, 1 -> 4, 1, 2, 3, 5
[3, 4, 5, 2, 1] -> 3, 5, 4, 2, 1 -> 3, 5, 1 , 2, 4  
```
```
    public void nextPermutation(int[] A) {
        if (A == null || A.length == 1) return;
    
     // 1. find num which break descending
        int i = A.length  - 2;
        while(i >= 0 && A[i] >= A[i + 1]) {
            i--;
        }

    // 2. find largest num between i and end of array
        if (i >= 0) {
            int j = A.length - 1;
            while(A[i] >= A[j]) {
                j--;
            }

            swap(A, i, j);
        }    
    
        reverse(A, i + 1, A.length - 1);
    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public void reverse(int[] nums, int i, int j) {
        while(i < j) {
            swap(nums, i, j);
            i++;
            j--;
        }
    }


```

My idea is for an array:
Start from its last element, traverse backward to find the first one with index i that satisfy num[i-1] < num[i]. So, elements from num[i] to num[n-1] is reversely sorted.

To find the next permutation, we have to swap some numbers at different positions, to minimize the increased amount, we have to make the highest changed position as high as possible. Notice that index larger than or equal to i is not possible as num[i,n-1] is reversely sorted. So, we want to increase the number at index i-1, clearly, swap it with the smallest number between num[i,n-1] that is larger than num[i-1]. For example, original number is 121543321, we want to swap the '1' at position 2 with '2' at position 7.

The last step is to make the remaining higher position part as small as possible, we just have to reversely sort the num[i,n-1]