Test stringhe ed array
This commit is contained in:
34
pom.xml
Normal file
34
pom.xml
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.fscotto</groupId>
|
||||||
|
<artifactId>interviewdemo</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.6.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>14</source>
|
||||||
|
<target>14</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package org.fscotto.interview.demo;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class InterviewQuestionsC1 {
|
||||||
|
|
||||||
|
public static boolean isUniqueChars(final String str) {
|
||||||
|
if (str == null || "".equals(str))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (str.length() > 128)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
final boolean[] charSet = new boolean[128];
|
||||||
|
for (int i = 0; i < str.length(); ++i) {
|
||||||
|
int val = str.charAt(i);
|
||||||
|
if (charSet[val])
|
||||||
|
return false;
|
||||||
|
charSet[val] = true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean permutation(final String s, final String t) {
|
||||||
|
if (s == null || t == null || s.length() != t.length())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
final int[] letters = new int[128];
|
||||||
|
final char[] sArray = s.toCharArray();
|
||||||
|
for (final char c : sArray)
|
||||||
|
letters[c]++;
|
||||||
|
|
||||||
|
for (int i = 0; i < t.length(); i++) {
|
||||||
|
final int c = t.charAt(i);
|
||||||
|
letters[c]--;
|
||||||
|
if (letters[c] < 0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void replaceSpaces(char[] str, int trueLength) {
|
||||||
|
int spaceCount = 0;
|
||||||
|
for (int i = 0; i < trueLength; i++) {
|
||||||
|
if (str[i] == ' ') {
|
||||||
|
spaceCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int index = trueLength + spaceCount * 2;
|
||||||
|
if (trueLength < str.length)
|
||||||
|
str[trueLength] = '\0';
|
||||||
|
for (int i = trueLength - 1; i >= 0; --i) {
|
||||||
|
if (str[i] == ' ') {
|
||||||
|
str[index - 1] = '0';
|
||||||
|
str[index - 2] = '2';
|
||||||
|
str[index - 3] = '%';
|
||||||
|
index -= 3;
|
||||||
|
} else {
|
||||||
|
str[index - 1] = str[i];
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean palindrome(String str) {
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package org.fscotto.interview.demo;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class InterviewQuestionsC1Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isUniqueCharsTests() {
|
||||||
|
assertFalse(InterviewQuestionsC1.isUniqueChars(null));
|
||||||
|
assertFalse(InterviewQuestionsC1.isUniqueChars(""));
|
||||||
|
assertTrue(InterviewQuestionsC1.isUniqueChars("a"));
|
||||||
|
assertTrue(InterviewQuestionsC1.isUniqueChars("abc"));
|
||||||
|
assertFalse(InterviewQuestionsC1.isUniqueChars("anna"));
|
||||||
|
assertFalse(InterviewQuestionsC1.isUniqueChars("birbante"));
|
||||||
|
assertFalse(InterviewQuestionsC1.isUniqueChars(getStringAllASCII()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getStringAllASCII() {
|
||||||
|
StringBuilder sb = new StringBuilder(130);
|
||||||
|
for (int i = 0; i < 129; ++i)
|
||||||
|
sb.append((char) i);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isPermutationOfTests() {
|
||||||
|
assertFalse(InterviewQuestionsC1.permutation(null, "BAC"));
|
||||||
|
assertFalse(InterviewQuestionsC1.permutation("BAC", null));
|
||||||
|
assertFalse(InterviewQuestionsC1.permutation(null, null));
|
||||||
|
assertTrue(InterviewQuestionsC1.permutation("", ""));
|
||||||
|
assertTrue(InterviewQuestionsC1.permutation("ABC", "BAC"));
|
||||||
|
assertTrue(InterviewQuestionsC1.permutation("BAC", "ABC"));
|
||||||
|
assertTrue(InterviewQuestionsC1.permutation("BAC", "CAB"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void replaceSpacesTests() {
|
||||||
|
final char[] str = "Mr John Smith ".toCharArray();
|
||||||
|
InterviewQuestionsC1.replaceSpaces(str, 13);
|
||||||
|
assertEquals("Mr%20John%20Smith", new String(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user