Minimum Distance of Two Words in a Sentence - Amazon Top Interview Questions


Problem Statement :


Given the strings text, word0, and word1, return the smallest distance between any two occurrences of word0 and word1 in text, measured in number of words in between. If either word0 or word1 doesn't appear in text, return -1.

Constraints

word0 and word1 are different.
n ≤ 200,000 where n is the length of text.

Example 1

Input
text = "dog cat hello cat dog dog hello cat world"

word0 = "hello"
word1 = "world"

Output
1

Explanation
There's only one word "cat" in between the hello and world at the end.



Solution :



title-img




                        Solution in C++ :

int solve(string text, string word0, string word1) {
    int i = -1, j = -1, k = 0;
    istringstream ss(text);
    string w;
    int ans = INT_MAX;
    while (ss >> w) {
        ++k;
        if (w == word0) {
            if (j != -1) ans = min(ans, k - j - 1);
            i = k;
        }
        if (w == word1) {
            if (i != -1) ans = min(ans, k - i - 1);
            j = k;
        }
    }
    return ans == INT_MAX ? -1 : ans;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(String text, String word0, String word1) {
        int w0 = -1;
        int w1 = -1;
        int min = Integer.MAX_VALUE;
        String[] words = text.split(" ");

        for (int i = 0; i < words.length; i++) {
            if (words[i].equals(word0)) {
                w0 = i;
            }

            if (words[i].equals(word1)) {
                w1 = i;
            }

            if (w0 != -1 && w1 != -1) {
                int diff = (int) Math.abs(w0 - w1) - 1;
                min = Math.min(min, diff);
            }
        }
        if (w0 == -1 || w1 == -1) {
            return -1;
        }

        return min;
    }
}
                    


                        Solution in Python : 
                            
int solve(string text, string word0, string word1) {
    int i = -1, j = -1, k = 0;
    istringstream ss(text);
    string w;
    int ans = INT_MAX;
    while (ss >> w) {
        ++k;
        if (w == word0) {
            if (j != -1) ans = min(ans, k - j - 1);
            i = k;
        }
        if (w == word1) {
            if (i != -1) ans = min(ans, k - i - 1);
            j = k;
        }
    }
    return ans == INT_MAX ? -1 : ans;
}
                    


View More Similar Problems

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →