
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find minimum distance of two given words in a text in Python
Suppose we have three strings text, w1, and w2. The text is a sentence with different words. We have to find the smallest distance between any two occurrences of w1 and w2 in the text, the distance is measured in number of words in between them. If either w1 or w2 is not present in text, return -1.
So, if the input is like text = "joy happy power happy joy joy power happy limit" w1 = "power" w2 = "limit", then the output will be 1, as there is only one word "happy" in between the power and limit.
To solve this, we will follow these steps −
index1 := null, index2 := null
distance := 999999
for each index idx and word w in text, do
if w is same as w1, then
if index2 is not null, then
distance := minimum of distance and (|idx - index2| - 1)
index1 := idx
if w is same as w2, then
if index1 is not null, then
distance := minimum of distance and (|idx - index1| - 1)
index2 := idx
if index1 is not null and index2 is not null, then
return distance
return -1
Example
Let us see the following implementation to get better understanding
def solve(text, w1, w2): index1 = None index2 = None distance = 2000000 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 text = "joy happy power happy joy joy power happy limit" w1 = "power" w2 = "limit" print(solve(text, w1, w2))
Input
"joy happy power happy joy joy power happy limit", "power", "limit"
Output
1
- Related Articles
- Smallest Distance Between Two Words in Python
- Program to find minimum cost to pick up gold in given two locations in Python
- Program to find the minimum edit distance between two strings in C++
- Python program to Count words in a given string?
- Program to find minimum distance to the target element using Python
- Python program to find uncommon words from two Strings
- Program to find minimum total distance between house and nearest mailbox in Python
- Program to find minimum elements to add to form a given sum in Python
- Program to find minimum swaps required to make given anagram in python
- Program to find minimum difference between two elements from two lists in Python
- How to Find the Shortest Words in a Text File using Python?
- How to Find the Longest Words in a Text File using Python?
- Program to find out distance between two nodes in a binary tree in Python
- Program to find largest distance pair from two list of numbers in Python
- Program to find minimum string size that contains given substring in Python
