Created
May 8, 2020 18:47
-
-
Save zeitan/ac7a61a111cd01b433f4b0d795477d0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static boolean areSame(String arr) | |
{ | |
char first = arr.charAt(0); | |
for (int i = 1; i < arr.length(); i++) | |
if (arr.charAt(i) != first) | |
return false; | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String findLongestV2(String inputData, int sequenceNumber) { | |
int fastPointer; | |
String longestString = ""; | |
int dataLength = inputData.length(); | |
//forward | |
for (int i = 0; i < dataLength; i += sequenceNumber) { | |
fastPointer = i + sequenceNumber; | |
if (i + sequenceNumber >= dataLength || fastPointer >= dataLength) | |
break; | |
String block = inputData.substring(i, i + sequenceNumber); | |
if (areSame(block)) { | |
String nextBlock = ""; | |
for (int j = fastPointer ; j < dataLength; j++) { | |
nextBlock = nextBlock + inputData.charAt(j); | |
if (!areSame(nextBlock)) | |
break; | |
block = block + inputData.charAt(j); | |
} | |
} | |
if (block.length() > longestString.length() ) | |
longestString = block; | |
} | |
//reverse | |
for (int i = inputData.length() -1 ; i > 0 ; i-= sequenceNumber) { | |
fastPointer = i - sequenceNumber; | |
if (i - sequenceNumber < 0 || fastPointer < 0) | |
break; | |
String block = inputData.substring(i - sequenceNumber, i ); | |
if (areSame(block)) { | |
String nextBlock = ""; | |
for (int j = fastPointer; j > 0; j--) { | |
nextBlock = nextBlock + inputData.charAt(j); | |
if (!areSame(nextBlock)) | |
break; | |
block = block + inputData.charAt(j); | |
} | |
} | |
if (block.length() > longestString.length() ) | |
longestString = block; | |
} | |
return longestString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment