Skip to content

Instantly share code, notes, and snippets.

@LazLondon
Last active September 15, 2016 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LazLondon/b4de74e7e51132ea2c9f43ee78c765fc to your computer and use it in GitHub Desktop.
Save LazLondon/b4de74e7e51132ea2c9f43ee78c765fc to your computer and use it in GitHub Desktop.
Getting the position and length of the longest consecutive repeating character in a form of "position,length" of the sample passed to the constructor.
package com.lazlondon.interview;
/**
* Created by Laz on 10/09/2016.
*/
import net.jcip.annotations.ThreadSafe;
/**
* Utility class for String manipulation.
*/
@ThreadSafe
public class StringUtility {
private final String sample;
public StringUtility(String sample) {
this.sample = sample;
}
/**
* Getting the position and length of the longest consecutive repeating character in a form of "position,length" of the sample passed to the constructor.
*
* @return -1 if the sample is null or empty string, otherwise it returns the position and length of the longest consecutive repeating character.
*/
public String getPosLenOfLCRC() {
if (isEmptySample()) {
return "-1";
}
return getPosLenOfLCRCWhenThereIsSample();
}
private boolean isEmptySample() {
return (sample == null) || (sample.length() == 0);
}
private String getPosLenOfLCRCWhenThereIsSample() {
LCRC resultLCRC = new LCRC(0, 1);
LCRC actualLCRC = new LCRC(1, 0);
while (actualLCRC.position < sample.length()) {
actualLCRC.length = getTheNumberOfConsecutiveRepeatationOfTheCharacterAtPosition(actualLCRC.position);
if (actualLCRC.length > resultLCRC.length) {
resultLCRC.setBy(actualLCRC);
}
actualLCRC.position += actualLCRC.length;
}
return resultLCRC.position + "," + resultLCRC.length;
}
/**
* The representation of the longest consecutive repeating character.
*/
private class LCRC {
private int position, length;
public LCRC(int position, int length) {
this.position = position;
this.length = length;
}
public void setBy(LCRC actualLCRC) {
position = actualLCRC.position;
length = actualLCRC.length;
}
}
private int getTheNumberOfConsecutiveRepeatationOfTheCharacterAtPosition(int i) {
int j = i;
while ((++j < sample.length()) && (sample.charAt(j) == sample.charAt(i))) ;
return j - i;
}
}
package com.lazlondon.interview;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by Laz on 10/09/2016.
*/
public class StringUtilityTest {
void assertLongestConsecutiveRepeatingCharacter(String expected, String sample) {
StringUtility stringUtility = new StringUtility(sample);
assertEquals(expected, stringUtility.getPosLenOfLCRC());
}
@Test
public void testEmptyString() {
assertLongestConsecutiveRepeatingCharacter("-1", "");
}
@Test
public void testNull() {
assertLongestConsecutiveRepeatingCharacter("-1", null);
}
@Test
public void testOneCharacterLength() {
assertLongestConsecutiveRepeatingCharacter("0,1", "a");
}
@Test
public void test2CharacterLength() {
assertLongestConsecutiveRepeatingCharacter("0,2", "aa");
}
@Test
public void test2DifferentCharacters() {
assertLongestConsecutiveRepeatingCharacter("0,1", "ab");
}
@Test
public void testABAA() {
assertLongestConsecutiveRepeatingCharacter("2,2", "abaa");
}
@Test
public void testAABBB() {
assertLongestConsecutiveRepeatingCharacter("2,3", "aabbb");
}
}
@LazLondon
Copy link
Author

Please feel free to do a code review. Any ideas how to make it more clean? (especially in the while loop)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment