commit
9a0eab7dac
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.algorithms.string;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class LongestSubstringNonRepeatingCharacters {
|
||||
|
||||
public static String getUniqueCharacterSubstringBruteForce(String input) {
|
||||
String output = "";
|
||||
for (int start = 0; start < input.length(); start++) {
|
||||
Set<Character> visited = new HashSet<>();
|
||||
int end = start;
|
||||
for (; end < input.length(); end++) {
|
||||
char currChar = input.charAt(end);
|
||||
if (visited.contains(currChar)) {
|
||||
break;
|
||||
} else {
|
||||
visited.add(currChar);
|
||||
}
|
||||
}
|
||||
if (output.length() < end - start + 1) {
|
||||
output = input.substring(start, end);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static String getUniqueCharacterSubstring(String input) {
|
||||
Map<Character, Integer> visited = new HashMap<>();
|
||||
String output = "";
|
||||
for (int start = 0, end = 0; end < input.length(); end++) {
|
||||
char currChar = input.charAt(end);
|
||||
if (visited.containsKey(currChar)) {
|
||||
start = Math.max(visited.get(currChar) + 1, start);
|
||||
}
|
||||
if (output.length() < end - start + 1) {
|
||||
output = input.substring(start, end + 1);
|
||||
}
|
||||
visited.put(currChar, end);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
if(args.length > 0) {
|
||||
System.out.println(getUniqueCharacterSubstring(args[0]));
|
||||
} else {
|
||||
System.err.println("This program expects command-line input. Please try again!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.algorithms.string;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static com.baeldung.algorithms.string.LongestSubstringNonRepeatingCharacters.getUniqueCharacterSubstring;
|
||||
import static com.baeldung.algorithms.string.LongestSubstringNonRepeatingCharacters.getUniqueCharacterSubstringBruteForce;
|
||||
|
||||
public class LongestSubstringNonRepeatingCharactersUnitTest {
|
||||
|
||||
@Test
|
||||
void givenString_whenGetUniqueCharacterSubstringBruteForceCalled_thenResultFoundAsExpectedUnitTest() {
|
||||
assertEquals("", getUniqueCharacterSubstringBruteForce(""));
|
||||
assertEquals("A", getUniqueCharacterSubstringBruteForce("A"));
|
||||
assertEquals("ABCDEF", getUniqueCharacterSubstringBruteForce("AABCDEF"));
|
||||
assertEquals("ABCDEF", getUniqueCharacterSubstringBruteForce("ABCDEFF"));
|
||||
assertEquals("NGISAWE", getUniqueCharacterSubstringBruteForce("CODINGISAWESOME"));
|
||||
assertEquals("be coding", getUniqueCharacterSubstringBruteForce("always be coding"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenGetUniqueCharacterSubstringCalled_thenResultFoundAsExpectedUnitTest() {
|
||||
assertEquals("", getUniqueCharacterSubstring(""));
|
||||
assertEquals("A", getUniqueCharacterSubstring("A"));
|
||||
assertEquals("ABCDEF", getUniqueCharacterSubstring("AABCDEF"));
|
||||
assertEquals("ABCDEF", getUniqueCharacterSubstring("ABCDEFF"));
|
||||
assertEquals("NGISAWE", getUniqueCharacterSubstring("CODINGISAWESOME"));
|
||||
assertEquals("be coding", getUniqueCharacterSubstring("always be coding"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue