Last active
January 10, 2016 03:19
-
-
Save hoangong/56b88fa2e770714434d6 to your computer and use it in GitHub Desktop.
Find the longest palindrome
This file contains 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
package com.ho.lp; | |
import java.util.Scanner; | |
/** | |
* Created by hoangong on 24/12/2015. | |
*/ | |
public class Main { | |
private String input; | |
public static void main(String[] args) { | |
Main app = new Main(); | |
app.processInput(); | |
} | |
public void processInput() { | |
System.out.println("Please enter a string"); | |
Scanner sc = new Scanner(System.in); | |
this.input = sc.nextLine(); | |
System.out.println("Checking string: " + input); | |
} | |
public void setInput(String input) { | |
this.input = input; | |
} | |
public String computeLongest() { | |
if (this.getInput().length() <= 2) | |
return this.getInput(); | |
else { | |
String longest = ""; | |
for (int i = 0; i < getInput().length(); i++) { | |
if (longest.equals("")) | |
longest = getInput().substring(i, i + 1); | |
Integer left = i - 1; | |
Integer right = i + 1; | |
longest = processSubString(left, right, longest); | |
if (longest.length() <= 1) | |
longest = getInput().substring(i, i + 2); | |
left = i - 1; | |
right = i + 2; | |
longest = processSubString(left, right, longest); | |
} | |
return longest; | |
} | |
} | |
String processSubString(Integer left, Integer right, String currentLongest) { | |
while (left >= 0 && right < getInput().length()) { | |
if (getInput().charAt(left) == getInput().charAt(right)) { | |
if (right - left > currentLongest.length()) { | |
currentLongest = getInput().substring(left, right + 1); | |
} | |
left--; | |
right++; | |
} else { | |
break; | |
} | |
} | |
return currentLongest; | |
} | |
public String getInput() { | |
return input; | |
} | |
} |
This file contains 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
package com.ho.lp; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.JUnit4; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.PrintStream; | |
import static org.junit.Assert.*; | |
/** | |
* Created by hoangong on 24/12/2015. | |
*/ | |
@RunWith(JUnit4.class) | |
public class MainTest { | |
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | |
private Main app; | |
@Before | |
public void setUpStreams() { | |
this.app = new Main(); | |
System.setOut(new PrintStream(outContent)); | |
} | |
@Test | |
public void startApplicationAskToInputString(){ | |
System.setIn(new ByteArrayInputStream("mystring".getBytes())); | |
app.processInput(); | |
assertEquals("Please enter a string\n" + | |
"Checking string: mystring\n",outContent.toString()); | |
} | |
@Test | |
public void returnEmptyIfInputIsEmpty(){ | |
app.setInput(""); | |
assertEquals("",app.computeLongest()); | |
} | |
@Test | |
public void returnStringIfInputLenghtIs1(){ | |
app.setInput("a"); | |
assertEquals("a",app.computeLongest()); | |
} | |
@Test | |
public void returnStringIfInputLenghtIs2(){ | |
app.setInput("ab"); | |
assertEquals("ab",app.computeLongest()); | |
} | |
@Test | |
public void returnLongestOnLeft(){ | |
app.setInput("asdffdsaoiash"); | |
assertEquals("asdffdsa",app.computeLongest()); | |
} | |
@Test | |
public void returnLongestOnRight(){ | |
app.setInput("oiashasdffdsa"); | |
assertEquals("asdffdsa",app.computeLongest()); | |
} | |
@Test | |
public void returnLongestOnMid(){ | |
app.setInput("iuhoiasdffdsaf"); | |
assertEquals("asdffdsa",app.computeLongest()); | |
} | |
@Test | |
public void returnLongestOnLeft2(){ | |
app.setInput("asdftfdsaoiash"); | |
assertEquals("asdftfdsa",app.computeLongest()); | |
} | |
@Test | |
public void returnLongestOnRight2(){ | |
app.setInput("oiashasdftfdsa"); | |
assertEquals("asdftfdsa",app.computeLongest()); | |
} | |
@Test | |
public void returnLongestOnMid2(){ | |
app.setInput("iuhoiasdftfdsaf"); | |
assertEquals("asdftfdsa",app.computeLongest()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment