using System.Collections.Generic;
public static void Main()
Console.WriteLine(CheckSorted(new string[]{"a","aa"}, "abcdefghijklmnopqrstuvwxyz"));
public static bool CheckSorted(string[] words, string order) {
var table = new Dictionary<char, int>();
for(int i = 0; i < order.Length; i++) {
table.Add(order[i], i + 1);
var priorSort = GetSortIndex(words[0], 0, table);
for (int i = 1; i < words.Length; i++) {
var wordSort = GetSortIndex(words[i], 0, table);
if (wordSort < priorSort) {
if (wordSort == priorSort) {
for (int j = 1; j < Math.Min(words[i].Length, words[i-1].Length) + 1; j++) {
var priorWordSort = GetSortIndex(words[i-1], j, table);
var currWordSort = GetSortIndex(words[i], j, table);
if (currWordSort < priorWordSort) {
else if (currWordSort > priorWordSort) {
private static int GetSortIndex(string word, int index, Dictionary<char, int> table) {
return word.Length <= index ? 0 : table[word[index]];