Closed Pankaj-Str closed 1 year ago
public class LongestSubstringWithoutRepeatingChars {
public static String findLongestSubstring(String s) {
if (s == null || s.isEmpty()) {
return "";
}
int n = s.length();
int maxLength = 0;
int start = 0;
int end = 0;
int[] charIndex = new int[256]; // Assuming ASCII characters
for (int i = 0, j = 0; j < n; j++) {
char currentChar = s.charAt(j);
i = Math.max(charIndex[currentChar], i);
if (j - i + 1 > maxLength) {
maxLength = j - i + 1;
start = i;
end = j;
}
charIndex[currentChar] = j + 1;
}
return s.substring(start, end + 1);
}
public static void main(String[] args) {
String input = "abcabcbb";
String longestSubstring = findLongestSubstring(input);
System.out.println("Longest substring without repeating characters: " + longestSubstring);
}
}