In this blog post, we'll take on an interesting coding challenge. It's about figuring out the correct order of letters.
Challenge: The Alien Dictionary
Imagine you have been given a list of words representing an Alien language sorted lexicographically from left to right. You need to determine the order of the characters in this alien language. Write a function alien_order(words) that takes a list of words as input and returns the Character order of the alien language. If no such order exists, return an empty string. You can assume that the input consists of lowercase letters and that there will be no duplicate words.
Related Articles
Input: words: A list of strings representing words in the alien language. (2
Output: A string representing the character order of the alien language.
Examples:
Input: words[] = {“baa”, “abcd”, “abca”, “cab”, “cad”}
Output: The order of characters is ‘b’, ‘d’, ‘a’, ‘c’
Explanation: Note that words are sorted and in the given language “baa” comes before “abcd”, therefore ‘b’ is before ‘a’ in output. Similarly, we can find other orders.
Input: words[] = {“caa”, “aaa”, “aab”}
Output: Order of characters is ‘c’, ‘a’, ‘b’
Alien Dictionary using Topological Sorting:
The approach to solving this challenge involves creating a directed graph, where each character represents a node, and edges between nodes indicate the relative order of characters. If the graph contains a cycle, a valid order of characters is not possible. Otherwise, we apply the topological sorting algorithm to determine the character order.
Following are the detailed steps:
The first step is to create a directed graph to represent the relative order of characters in the alien language.
Each character in the language corresponds to a node in the graph, and edges between nodes indicate the order of characters.
Step 2: Iterate Through Adjacent Words
Step 3: Add Edges to the Graph
Add directed edges in the graph based on the character order discovered in Step 2.
For each mismatched character in word1 and word2, add an edge from the character in word1 to the character in word2.
Step 4: Check for Cycles
To determine if a valid character order is possible, check if the graph contains any cycles.
Step 5: Perform Topological Sorting
If the graph is a Directed Acyclic Graph (DAG), perform topological sorting on the graph. Print the characters in topological order as the final output.
Solution in Java:
import java.util.*;
public class AlienDictionarySolver {
// Function to add a directed edge between characters u and v
private static void addEdge(List[] adj, int u, int v) {
adj[u].add(v);
}
// Depth-First Search (DFS) function to check for cycles in the graph
private static boolean dfs(List[] adj, int u, boolean[] visited, boolean[] recStack) {
visited[u] = true;
recStack[u] = true;
for (int v : adj[u]) {
if (!visited[v]) {
if (dfs(adj, v, visited, recStack)) {
return true;
}
} else if (recStack[v]) {
return true;
}
}
recStack[u] = false;
return false;
}
// Function to check if a cycle exists in the graph using DFS
private static boolean checkCycle(List[] adj, int V) {
boolean[] visited = new boolean[V];
boolean[] recStack = new boolean[V];
for (int i = 0; i [] adj, int u, boolean[] visited, Stack stack) {
visited[u] = true;
for (int v : adj[u]) {
if (!visited[v]) {
topologicalSortUtil(adj, v, visited, stack);
}
}
stack.push(u);
}
// Function to perform topological sorting
private static void topologicalSort(List[] adj, int V) {
Stack stack = new Stack();
boolean[] visited = new boolean[V];
for (int i = 0; i [] adj = new List[k];
for (int i = 0; i ();
}
// Build the graph by iterating through adjacent words
for (int i = 0; i