Number of Concatenations to Create Subsequence- Google Top Interview Questions


Problem Statement :


You are given two lowercase alphabet strings s and t. 

Return the minimum number of times we must concatenate s such that t is a subsequence of s. 

For example, if we concatenate "abc" three times, we'd get "abcabcabc". If it's not possible, return -1.

Constraints

1 ≤ n ≤ 100,000 where n is the length of s

1 ≤ m ≤ 100,000 where m is the length of t

Example 1

Input

s = "dab"

t = "abbd"

Output

3

Explanation

If we concatenate a = "dab" three times, we can get "dabdabdab". And "abbd" is a subsequence of 
"dabdabdab".



Example 2

Input

s = "abc"

t = "def"

Output

-1

Explanation

It's impossible to make t a subsequence of s.



Solution :



title-img




                        Solution in C++ :

int solve(string s, string t) {
    vector<vector<int>> v(128);
    for (int i = 0; i < s.length(); i++) {
        v[s[i]].push_back(i);
    }
    int ans = 1, j = 0, currind = -1;
    while (j < t.length()) {
        vector<int> &arr = v[t[j]];
        if (arr.size() == 0) return -1;
        int nextind = upper_bound(arr.begin(), arr.end(), currind) - arr.begin();
        if (nextind == arr.size()) {
            currind = -1;
            ans += 1;
        } else {
            currind = arr[nextind];
            j += 1;
        }
    }
    return j == t.length() ? ans : -1;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    int v(char c) {
        return (int) (c - 'a');
    }
    public int solve(String s, String t) {
        int m = s.length(), n = t.length();
        Integer[][] indices = new Integer[m][26];
        indices[m - 1][v(s.charAt(m - 1))] = m - 1;
        for (int i = m - 2; i >= 0; i--) {
            int c = v(s.charAt(i));
            for (int j = 0; j < 26; j++) indices[i][j] = indices[i + 1][j];
            indices[i][c] = i;
        }
        int i = 0, j = 0;

        int use = 1;
        while (j < n) {
            int c = v(t.charAt(j));
            if (indices[0][c] == null)
                return -1;
            if (indices[i % m][c] == null) {
                i = 0;
                use++;
            } else {
                i = indices[i % m][c] + 1;
                if (i >= m) {
                    i = 0;
                    use++;
                }
                j++;
            }
        }
        if (i == 0)
            return use - 1;
        return use;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, s, t):
        if not set(t).issubset(set(s)):
            return -1

        pos = defaultdict(list)
        for i, c in enumerate(s):
            pos[c].append(i)

        def match(t_i):
            cache = t_i
            prev_pos = -1
            while t_i < len(t):
                s_i = bisect_right(pos[t[t_i]], prev_pos)
                if s_i == len(pos[t[t_i]]):
                    break

                prev_pos = pos[t[t_i]][s_i]
                t_i += 1

            return t_i

        ans = 0
        t_i = 0
        while t_i < len(t):
            t_i = match(t_i)
            ans += 1
        return ans
                    


View More Similar Problems

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →