Advertisement
rajeshinternshala

Untitled

Mar 7th, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1.   public static int longestSubstringWithoutForbidden(String s, String[] forbidden) {
  2.         int max = 0;
  3.         s=s.toLowerCase()
  4.         HashSet<String> set = new HashSet<String>();
  5.         for (String m : forbidden) {
  6.             set.add(m);
  7.         }
  8.         int n = s.length();
  9.         int[] a = new int[n + 1];
  10.         Arrays.fill(a, n);
  11.         for (int i = 0; i < n; ++i) {
  12.             String t = "";
  13.             for (int j = 0; i + j < n && j < 10; ++j) {
  14.                 t += s.charAt(i + j);
  15.                 if (set.contains(t)) {
  16.                     a[i] = Math.min(a[i], (i + j));
  17.                     break;
  18.                 }
  19.             }
  20.         }
  21.         int answer = 0;
  22.         for (int i = n - 1; i >= 0; i--) {
  23.             a[i] = Math.min(a[i], a[i + 1]);
  24.             answer = Math.max(answer, a[i] - i);
  25.         }
  26.         return answer;
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement