Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Ancient Astronaut Theory in Python
The Ancient Astronaut Theory problem involves checking if a string follows a custom lexicographic ordering defined by an ancient astronaut dictionary. Given a dictionary string that represents the order of characters, we need to verify if the input string is sorted according to this custom order.
For example, if the dictionary is "bdc", then 'b' comes before 'd', and 'd' comes before 'c' in this custom ordering.
Problem Understanding
Given a dictionary string and an input string, we need to check if the characters in the input string appear in the same order as defined by the dictionary. Characters not in the dictionary are ignored.
Example
If dictionary = "bdc" and s = "bbbb h ddd i cccc", the output should be True because:
First we encounter 'b' characters (position 0 in dictionary)
Then we encounter 'd' characters (position 1 in dictionary)
Finally we encounter 'c' characters (position 2 in dictionary)
Characters 'h', ' ', and 'i' are ignored as they're not in the dictionary
Algorithm
To solve this problem, we follow these steps:
Initialize a pointer to track the current position in the dictionary
For each character in the input string, check if it exists in the dictionary
If the character is found, advance the pointer to that character's position
If we encounter a character that appears earlier in the dictionary than our current position, return False
Return True if all characters follow the correct order
Implementation
class Solution:
def solve(self, astro_dict, s):
l = len(astro_dict)
if l == 0:
return True
i = 0
for c in s:
if c in astro_dict:
while i < l and astro_dict[i] != c:
i += 1
if i >= l or astro_dict[i] != c:
return False
return True
# Test the solution
ob = Solution()
print(ob.solve("bdc", "bbbb h ddd i cccc"))
print(ob.solve("abc", "aabbcc"))
print(ob.solve("abc", "acb"))
True True False
How It Works
The algorithm maintains a pointer i that tracks the current position in the dictionary. For each character in the input string:
If the character exists in the dictionary, we advance the pointer to find it
If we can't find the character at or after the current position, the string is not sorted
Characters not in the dictionary are simply ignored
Test Cases
class Solution:
def solve(self, astro_dict, s):
l = len(astro_dict)
if l == 0:
return True
i = 0
for c in s:
if c in astro_dict:
while i < l and astro_dict[i] != c:
i += 1
if i >= l or astro_dict[i] != c:
return False
return True
# Various test cases
ob = Solution()
# Test case 1: Original example
print("Test 1:", ob.solve("bdc", "bbbb h ddd i cccc"))
# Test case 2: Empty dictionary
print("Test 2:", ob.solve("", "any string"))
# Test case 3: Reverse order (should be False)
print("Test 3:", ob.solve("abc", "cba"))
# Test case 4: Correct order
print("Test 4:", ob.solve("xyz", "xxxyyyzzz"))
Test 1: True Test 2: True Test 3: False Test 4: True
Conclusion
The Ancient Astronaut Theory problem is solved by tracking character positions in a custom dictionary order. The algorithm efficiently checks lexicographic sorting with O(n) time complexity, where n is the length of the input string.
