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

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →