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
Program to find minimum distance of two given words in a text in Python
Finding the minimum distance between two words in a text is a common string processing problem. The distance is measured as the number of words between any occurrence of the two target words.
Given a text string and two words w1 and w2, we need to find the smallest number of words between any occurrences of these words. If either word is not present, we return -1.
Algorithm
The approach involves tracking the most recent positions of both words as we iterate through the text ?
Initialize
index1andindex2asNoneSet
distanceto a large value initially-
For each word in the text:
If word matches
w1, updateindex1and calculate distance withindex2if availableIf word matches
w2, updateindex2and calculate distance withindex1if available
Return the minimum distance found, or
-1if either word is missing
Example
Let's implement the solution to find minimum word distance ?
def solve(text, w1, w2):
index1 = None
index2 = None
distance = float('inf')
for idx, word in enumerate(text.split(" ")):
if word == w1:
if index2 is not None:
distance = min(distance, abs(idx - index2) - 1)
index1 = idx
if word == w2:
if index1 is not None:
distance = min(distance, abs(idx - index1) - 1)
index2 = idx
if index1 is not None and index2 is not None:
return distance
return -1
# Test the function
text = "joy happy power happy joy joy power happy limit"
w1 = "power"
w2 = "limit"
result = solve(text, w1, w2)
print(f"Minimum distance between '{w1}' and '{w2}': {result}")
Minimum distance between 'power' and 'limit': 1
How It Works
In the example text "joy happy power happy joy joy power happy limit":
power appears at indices 2 and 6
limit appears at index 8
Distance between power (index 6) and limit (index 8) = |8 - 6| - 1 = 1
There is one word "happy" between them
Edge Cases
Let's test when one or both words are missing ?
def solve(text, w1, w2):
index1 = None
index2 = None
distance = float('inf')
for idx, word in enumerate(text.split(" ")):
if word == w1:
if index2 is not None:
distance = min(distance, abs(idx - index2) - 1)
index1 = idx
if word == w2:
if index1 is not None:
distance = min(distance, abs(idx - index1) - 1)
index2 = idx
if index1 is not None and index2 is not None:
return distance
return -1
# Test edge cases
print(solve("hello world python", "hello", "missing")) # -1 (missing word)
print(solve("cat dog cat dog", "cat", "dog")) # 0 (adjacent words)
print(solve("one two three four", "one", "four")) # 2 (two words between)
-1 0 2
Conclusion
This algorithm efficiently finds the minimum distance between two words by tracking their most recent positions during a single pass through the text. The time complexity is O(n) where n is the number of words, making it optimal for this problem.
