diff --git a/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharacters.java b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharacters.java new file mode 100644 index 0000000000..cd1f3e94d5 --- /dev/null +++ b/algorithms-miscellaneous-1/src/main/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharacters.java @@ -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 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 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!"); + } + } + +} diff --git a/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharactersUnitTest.java b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharactersUnitTest.java new file mode 100644 index 0000000000..9f1e6a2519 --- /dev/null +++ b/algorithms-miscellaneous-1/src/test/java/com/baeldung/algorithms/string/LongestSubstringNonRepeatingCharactersUnitTest.java @@ -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")); + } + +}