Renamed method names to replace "non-repeating" with "Unique".

This commit is contained in:
akash.pandey 2018-12-01 09:08:53 +05:30
parent 09261fb3b9
commit ed0713046f
2 changed files with 71 additions and 71 deletions

View File

@ -7,7 +7,7 @@ import java.util.Set;
public class LongestSubstringNonRepeatingCharacters {
public static String getNonRepeatingCharactersBruteForce(String input) {
public static String getUniqueCharacterSubstringBruteForce(String input) {
String output = "";
for (int start = 0; start < input.length(); start++) {
Set<Character> visited = new HashSet<>();
@ -27,16 +27,16 @@ public class LongestSubstringNonRepeatingCharacters {
return output;
}
public static String getNonRepeatingCharacters(String input) {
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 (visited.containsKey(currChar)) {
start = Math.max(visited.get(currChar) + 1, start);
}
if(output.length() < end - start + 1) {
output = input.substring(start, end+1);
if (output.length() < end - start + 1) {
output = input.substring(start, end + 1);
}
visited.put(currChar, end);
}
@ -45,7 +45,7 @@ public class LongestSubstringNonRepeatingCharacters {
public static void main(String[] args) {
String input = "CODINGISAWESOME";
System.out.println(getNonRepeatingCharacters(input));
System.out.println(getUniqueCharacterSubstring(input));
}
}

View File

@ -6,15 +6,15 @@ import org.junit.jupiter.api.Test;
public class LongestSubstringNonRepeatingCharactersTest {
@Test
void givenString_whenGetNonRepeatingCharactersBruteForceCalled_thenResultFoundAsExpected() {
void givenString_whenGetUniqueCharacterSubstringBruteForceCalled_thenResultFoundAsExpected() {
String input = "CODINGISAWESOME";
Assertions.assertEquals("NGISAWE", LongestSubstringNonRepeatingCharacters.getNonRepeatingCharactersBruteForce(input));
Assertions.assertEquals("NGISAWE", LongestSubstringNonRepeatingCharacters.getUniqueCharacterSubstringBruteForce(input));
}
@Test
void givenString_whenGetNonRepeatingCharactersCalled_thenResultFoundAsExpected() {
void givenString_whenGetUniqueCharacterSubstringCalled_thenResultFoundAsExpected() {
String input = "CODINGISAWESOME";
Assertions.assertEquals("NGISAWE",LongestSubstringNonRepeatingCharacters.getNonRepeatingCharacters(input));
Assertions.assertEquals("NGISAWE",LongestSubstringNonRepeatingCharacters.getUniqueCharacterSubstring(input));
}
}