using
System;
using
System.Collections.Generic;
namespace
Anagrams {
class
Substring {
public
int
[] count;
public
Substring()
{
count =
new
int
[26];
Array.Fill(count, 0);
}
public
override
bool
Equals(
object
obj)
{
Substring other = obj
as
Substring;
if
(other ==
null
) {
return
false
;
}
for
(
int
i = 0; i < 26; i++) {
if
(other.count[i] != count[i]) {
return
false
;
}
}
return
true
;
}
public
override
int
GetHashCode()
{
const
int
MOD = 1000000007;
int
hash = 0;
for
(
int
i = 0; i < 26; i++) {
hash += (i + 1) * count[i];
hash %= MOD;
}
return
hash;
}
}
class
Program {
static
void
CheckForAnagrams(
string
s,
int
n,
char
X,
int
k)
{
bool
found =
false
;
int
[, ] prefix =
new
int
[n + 1, 26];
for
(
int
i = 0; i < n; i++) {
prefix[i, s[i] -
'a'
] += 1;
}
for
(
int
i = 1; i < n; i++) {
for
(
int
j = 0; j < 26; j++) {
prefix[i, j] += prefix[i - 1, j];
}
}
Dictionary<Substring,
int
> map
=
new
Dictionary<Substring,
int
>();
for
(
int
i = 0; i < n; i++) {
if
(i + k > n) {
break
;
}
Substring sub =
new
Substring();
for
(
int
j = 0; j < 26; j++) {
sub.count[j]
= prefix[i + k - 1, j]
- (i - 1 >= 0 ? prefix[i - 1, j] : 0);
}
if
(sub.count[X -
'a'
] != 0) {
continue
;
}
if
(map.ContainsKey(sub)) {
found =
true
;
Console.WriteLine(s.Substring(map[sub], k)
+
" "
+ s.Substring(i, k));
break
;
}
else
{
map[sub] = i;
}
}
if
(!found) {
Console.WriteLine(
"-1"
);
}
}
static
void
Main(
string
[] args)
{
string
s =
"rotator"
;
int
n = s.Length;
char
X =
'a'
;
int
k = 3;
CheckForAnagrams(s, n, X, k);
}
}
}