using
System;
using
System.Collections.Generic;
public
class
GFG {
static
bool
patternMatchUtil(
string
str,
int
n,
int
i,
string
pat,
int
m,
int
j,
Dictionary<
char
,
string
> map)
{
if
(i == n && j == m)
return
true
;
if
(i == n || j == m)
return
false
;
char
ch = pat[j];
if
(map.ContainsKey(ch)) {
string
s = map[ch];
int
len = s.Length;
if
(len <= n - i) {
string
subStr = str.Substring(i, len);
if
(subStr != s)
return
false
;
return
patternMatchUtil(str, n, i + len, pat, m, j + 1, map);
}
else
{
return
false
;
}
}
for
(
int
len = 1; len <= n - i; len++) {
map[ch] = str.Substring(i, len);
if
(patternMatchUtil(str, n, i + len, pat, m, j + 1, map))
return
true
;
map.Remove(ch);
}
return
false
;
}
static
bool
patternMatch(
string
str,
string
pat,
int
n,
int
m) {
if
(n < m)
return
false
;
Dictionary<
char
,
string
> map =
new
Dictionary<
char
,
string
>();
bool
res = patternMatchUtil(str, n, 0, pat, m, 0, map);
foreach
(
var
item
in
map) {
Console.WriteLine(item.Key +
"->"
+ item.Value);
}
return
res;
}
static
public
void
Main(
string
[] args) {
string
str =
"GeeksforGeeks"
;
string
pat =
"GfG"
;
int
n = str.Length;
int
m = pat.Length;
if
(!patternMatch(str, pat, n, m))
Console.WriteLine(
"No Solution exists"
);
}
}