Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int longestSubstringWithoutForbidden(String s, String[] forbidden) {
- int max = 0;
- s=s.toLowerCase()
- HashSet<String> set = new HashSet<String>();
- for (String m : forbidden) {
- set.add(m);
- }
- int n = s.length();
- int[] a = new int[n + 1];
- Arrays.fill(a, n);
- for (int i = 0; i < n; ++i) {
- String t = "";
- for (int j = 0; i + j < n && j < 10; ++j) {
- t += s.charAt(i + j);
- if (set.contains(t)) {
- a[i] = Math.min(a[i], (i + j));
- break;
- }
- }
- }
- int answer = 0;
- for (int i = n - 1; i >= 0; i--) {
- a[i] = Math.min(a[i], a[i + 1]);
- answer = Math.max(answer, a[i] - i);
- }
- return answer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement