Distance between two words python


Suppose we have two strings, word0, and word1 and a text. We have to find the smallest distance between any two occurrences of word0 and word1 in the given text. Here the distance is measured in number of words. If they are not present in the text then return -1.

So, if the input is like text = "cat dog abcd dog cat cat abcd dog wxyz", word0 = "abcd", word1 = "wxyz", then the output will be 1, as there is one word "dog" between "abcd" and "wxyz"

To solve this, we will follow these steps −

  • word_list := a list of words from text
  • ans := size of word_list
  • L := null
  • for R in range 0 to size of word_list - 1, do
    • if word_list[R] is word0 or word_list[R] is word1, then
      • if L is not null and word_list[R] is not word_list[L], then
        • ans := minimum of ans and R - L - 1
      • L := R
  • return -1 if ans is same as size of word_list otherwise ans

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, text, word0, word1):
      word_list = text.split()
      ans = len(word_list)
      L = None
      for R in range(len(word_list)):
         if word_list[R] == word0 or word_list[R] == word1:
            if L is not None and word_list[R] != word_list[L]:
               ans = min(ans, R - L - 1)
               L = R
      return -1 if ans == len(word_list) else ans
ob = Solution()
text = "cat dog abcd dog cat cat abcd dog wxyz"
word0 = "abcd"
word1 = "wxyz"
print(ob.solve(text, word0, word1))

Input

"cat dog abcd dog cat cat abcd dog wxyz", "abcd", "wxyz"

Output

1

Distance between two words python

Updated on 22-Sep-2020 11:25:39

  • Related Questions & Answers
  • Calculate distance and duration between two places using google distance matrix API in Python?
  • Program to find minimum distance of two given words in a text in Python
  • Hamming Distance between two strings in JavaScript
  • Check if edit distance between two strings is one in Python
  • Remove space between two words in MySQL?
  • C program to calculate distance between two points
  • Find the minimum distance between two numbers in C++
  • Common Words in Two Strings in Python
  • Find K-th Smallest Pair Distance in C++
  • Program to find out distance between two nodes in a binary tree in Python
  • How to compute pairwise distance between two vectors in PyTorch?
  • 8085 program to find smallest number between two numbers
  • Program to find smallest pair sum where distance is not consecutive in Python
  • How to get the distance between two geographic locations in Android?
  • Find distance between two nodes of a Binary Tree in C++

How do you find the distance between two words in Python?

Smallest Distance Between Two Words in Python.
word_list := a list of words from text..
ans := size of word_list..
L := null..
for R in range 0 to size of word_list - 1, do. if word_list[R] is word0 or word_list[R] is word1, then. ... .
return -1 if ans is same as size of word_list otherwise ans..

How do you find the distance between two strings?

There are several ways to measure the distance between two strings. The simplest one is to use hamming distance to find the number of mismatch between two strings. However, the two strings must have the same length.

How do you find the distance between two characters in Python?

You can just use the str. index(char, [beg, end]) method to retrieve the position of a character inside a string. The method allows you to add a beginning and end position that you can use to check for the different occurrences. Using multiple lines you could avoid calling the s.

What is levenshtein distance in Python?

Levenshtein distance is a lexical similarity measure which identifies the distance between one pair of strings. It does so by counting the number of times you would have to insert, delete or substitute a character from string 1 to make it like string 2.