1

My requirement is to find if a unknown string of 4 chars is repeating of a string of length 16.

So basically, INPUT : "shgtHellorhecHello" Should return true.

Only known thing to us is that the repeating substring if of 4 chars and the full string is if 16 chars.

Please help

1
  • Did you mean we have just the string and size of substring as inputs params and not the actual substring? Also, could you paste what you have tried so far? Commented Apr 25, 2020 at 19:39

1 Answer 1

3

You can do this with a Regular Expression:

String searchString = 'shgtHellorhecHello';
Pattern p = Pattern.compile('(.{4}).*\\1');
Boolean repeatFound = p.matcher(searchString).find();

This pattern matches "any four characters ((.{4})), separated by any number of characters (.*), then those same four characters from the first group (\\1)".

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.