All Tracks Algorithms String Algorithms String Searching Problem

Solitary String
Tag(s):

Easy, Strings

Problem
Editorial
Analytics

In order to celebrate Women's day, Stella started playing with string $$S$$. She needs to know the maximum number of characters between any $$2$$ same characters in the string.

As she is busy in playing, help her for the same.

If there are no $$2$$ same characters in the string, print $$-1$$.

Note: String is composed of lowercase letters of the Latin alphabet.

Input:
First line contains one integer $$T$$, denoting the number of test cases.
Each of the next $$T$$ line contains one string $$S$$.

Output:
For each test case, output the maximum number of characters between any $$2$$ same characters in the string. If there are no $$2$$ same characters in the string, print $$-1$$.
Print answer for each test case in a new line.

Constraints:
$$1 \le T \le 10$$
$$ 1 ≤ |S| ≤ 10^5$$, where $$|S| $$ determines the length of the string.
String is composed of lowercase alphabets ranging from $$a$$ to $$z$$.

SAMPLE INPUT
2
aba
babcddc
SAMPLE OUTPUT
1
2
Explanation

Here, for string = $$aba$$

There is only one character between 2 occurrences of $$a$$.

2) For string = $$babcddc$$

There is one character between 2 occurrences of $$b$$, and 2 characters between 2 occurrences of $$c$$. So the answer is $$2$$.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala 2.11.8, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

International Women's Hackathon

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications