Created
May 27, 2019 17:34
-
-
Save madhur/c79542677572b08985b6df95984e716f to your computer and use it in GitHub Desktop.
Longest non repeating substring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static int longestNRSubstringLen(String input) { | |
| if(input==null) | |
| return 0; | |
| char[] array = input.toCharArray(); | |
| int prev = 0; | |
| HashMap<Character, Integer> characterMap = new HashMap<Character, Integer>(); | |
| for (int i = 0; i < array.length; i++) { | |
| if (!characterMap.containsKey(array[i])) { | |
| characterMap.put(array[i], i); | |
| } else { | |
| prev = Math.max(prev, characterMap.size()); | |
| i = characterMap.get(array[i]); | |
| characterMap.clear(); | |
| } | |
| } | |
| return Math.max(prev, characterMap.size()); | |
| } | |
| // java.util.* and java.util.streams.* have been imported for this problem. | |
| // You don't need any other imports. | |
| public static int longestNRSubstringLen(String input) { | |
| if (input == null || input.length()==0) { | |
| return 0; | |
| } | |
| int answer = 0; | |
| int maxAnswer = 0; | |
| for(int i=0; i<input.length(); ++i) { | |
| answer=1; | |
| for(int j=i+1; j<input.length(); ++j) { | |
| if(input.charAt(i)==input.charAt(j)) { | |
| break; | |
| } | |
| answer++; | |
| } | |
| if(maxAnswer < answer) { | |
| maxAnswer = answer; | |
| } | |
| } | |
| return maxAnswer; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment