Write palindrome function with tests
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -24,8 +24,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>14</source>
|
<source>8</source>
|
||||||
<target>14</target>
|
<target>8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
package org.fscotto.interview.demo;
|
package org.fscotto.interview.demo;
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class InterviewQuestionsC1 {
|
public class InterviewQuestionsC1 {
|
||||||
|
|
||||||
public static boolean isUniqueChars(final String str) {
|
public static boolean isUniqueChars(final String s) {
|
||||||
if (str == null || "".equals(str))
|
if (s == null || "".equals(s))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (str.length() > 128)
|
if (s.length() > 128)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
final boolean[] charSet = new boolean[128];
|
final boolean[] charSet = new boolean[128];
|
||||||
for (int i = 0; i < str.length(); ++i) {
|
for (int i = 0; i < s.length(); ++i) {
|
||||||
int val = str.charAt(i);
|
int val = s.charAt(i);
|
||||||
if (charSet[val])
|
if (charSet[val])
|
||||||
return false;
|
return false;
|
||||||
charSet[val] = true;
|
charSet[val] = true;
|
||||||
@@ -39,33 +37,38 @@ public class InterviewQuestionsC1 {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void replaceSpaces(char[] str, int trueLength) {
|
public static void replaceSpaces(char[] chars, int trueLength) {
|
||||||
int spaceCount = 0;
|
int spaceCount = 0;
|
||||||
for (int i = 0; i < trueLength; i++) {
|
for (int i = 0; i < trueLength; i++) {
|
||||||
if (str[i] == ' ') {
|
if (chars[i] == ' ') {
|
||||||
spaceCount++;
|
spaceCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = trueLength + spaceCount * 2;
|
int index = trueLength + spaceCount * 2;
|
||||||
if (trueLength < str.length)
|
if (trueLength < chars.length)
|
||||||
str[trueLength] = '\0';
|
chars[trueLength] = '\0';
|
||||||
for (int i = trueLength - 1; i >= 0; --i) {
|
for (int i = trueLength - 1; i >= 0; --i) {
|
||||||
if (str[i] == ' ') {
|
if (chars[i] == ' ') {
|
||||||
str[index - 1] = '0';
|
chars[index - 1] = '0';
|
||||||
str[index - 2] = '2';
|
chars[index - 2] = '2';
|
||||||
str[index - 3] = '%';
|
chars[index - 3] = '%';
|
||||||
index -= 3;
|
index -= 3;
|
||||||
} else {
|
} else {
|
||||||
str[index - 1] = str[i];
|
chars[index - 1] = chars[i];
|
||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean palindrome(String str) {
|
public static boolean palindrome(String s) {
|
||||||
|
final String str = s.toLowerCase();
|
||||||
return false;
|
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
|
||||||
|
if (str.charAt(i) != str.charAt(j)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,12 @@ class InterviewQuestionsC1Test {
|
|||||||
assertEquals("Mr%20John%20Smith", new String(str));
|
assertEquals("Mr%20John%20Smith", new String(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void palindromeTests() {
|
||||||
|
assertTrue(InterviewQuestionsC1.palindrome("anna"));
|
||||||
|
assertTrue(InterviewQuestionsC1.palindrome("radar"));
|
||||||
|
assertTrue(InterviewQuestionsC1.palindrome("level"));
|
||||||
|
assertFalse(InterviewQuestionsC1.palindrome("dog"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user