# 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 :

```                        ```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];
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[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```
```

## 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

## 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 -

## 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

## 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

## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.