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. Show 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 −
ExampleLet 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" Output1
Updated on 12-Oct-2021 11:18:51
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 −
Let us see the following implementation to get better understanding − ExampleLive 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" Output1
Updated on 22-Sep-2020 11:25:39
How do you find the distance between two words in Python?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.. 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.
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 does Python calculate edit distance?The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. For example, the edit distance between "kitten" and "sitting" is three: substitute the "k" for "s", substitute the "e" for "i", and append a "g".
|