class
GFG
{
static
int
findMinimumSubsequences(String A,
String B)
{
int
numberOfSubsequences =
1
;
int
sizeOfB = B.length();
int
sizeOfA = A.length();
int
inf =
1000000
;
int
[][]next =
new
int
[
26
][sizeOfB];
for
(
int
i =
0
; i <
26
; i++) {
for
(
int
j =
0
; j < sizeOfB; j++) {
next[i][j] = inf;
}
}
for
(
int
i =
0
; i < sizeOfB; i++) {
next[B.charAt(i) -
'a'
][i] = i;
}
for
(
int
i =
0
; i <
26
; i++) {
for
(
int
j = sizeOfB -
2
; j >=
0
; j--) {
if
(next[i][j] == inf) {
next[i][j] = next[i][j +
1
];
}
}
}
int
pos =
0
;
int
i =
0
;
while
(i < sizeOfA) {
if
(pos ==
0
&&
next[A.charAt(i)-
'a'
][pos] == inf) {
numberOfSubsequences = -
1
;
break
;
}
else
if
(pos < sizeOfB &&
next[A.charAt(i) -
'a'
][pos] < inf) {
int
nextIndex = next[A.charAt(i) -
'a'
][pos] +
1
;
pos = nextIndex;
i++;
}
else
{
numberOfSubsequences++;
pos =
0
;
}
}
return
numberOfSubsequences;
}
public
static
void
main(String[] args)
{
String A =
"aacbe"
;
String B =
"aceab"
;
System.out.print(findMinimumSubsequences(A, B));
}
}