Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Length of longest substring
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; /* Input: "pwwkew" Output: 3 */ class Rextester { public static void main(String args[]) { System.out.println("Hello, World!"); System.out.println(lengthOfLongestSubstring(new String("paqwmncwkew"))); } public static int lengthOfLongestSubstring(String s) { int left = 0; int right = 0; int[] arr = new int[256]; int max = 0; while(right<s.length()){ while(arr[s.charAt(right)] > 0) { arr[s.charAt(left)]--; left++; } arr[s.charAt(right)]++; max = Math.max(max, right-left+1); right++; } return max; } public static int lengthOfLongestSubstringSlow(String s) { Set<Character> set = new HashSet<>(); int count=1,longest=0; for(int i = 0;i<s.length();i++){ set.add(s.charAt(i)); for(int j = i+1;j<s.length();j++){ if(set.contains(s.charAt(j))) break; set.add(s.charAt(j)); count+=1; } if(count>longest) longest = count; count=1; set.clear(); } return longest; } }
run
|
edit
|
history
|
help
0
Sorting array
overloading
Eckhart generator with real id
Zombie virus
Increment Operator
Interference
Item battle rolyal
메소드구현하기-해결
Fibonacci numbers 0 - 94
Bank System