Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
1.4 kB
2
Indexable
Never
int n = forbidden.size(), min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        Set<String> forbs = new HashSet<>();
        for (String temp : forbidden) {
            forbs.add(temp);
            if (temp.length() < min)
                min = temp.length();
            if (temp.length() > max)
                max = temp.length();
        }
        //System.out.println("min = " + min + " max = " + max);
        int end = 0, start = 0;
        char[] chars = word.toCharArray();
        int ans = 0;
        while(end < chars.length){
            //System.out.println("11-length = " + ans + ", start = " + start + ", end = " + end);
            int leftEnd = end - (min - 1);
            boolean tripWire = true;
            while(leftEnd >= end - (max - 1) && leftEnd >= start) {
                String temp = new String(chars, leftEnd, end - leftEnd + 1);
                if(forbs.contains(temp)) {
                    start = leftEnd + 1;
                    end = Math.max(start, end);
                    tripWire = false;
                    break;
                }
                leftEnd--;
            }
            ans = Math.max(ans, end - start + 1);
            //System.out.println("length = " + ans + ", start = " + start + ", end = " + end);
            // System.out.println(end - start + 1 + " = " + new String(chars, start, end - start + 1));
            if (tripWire)
                end++;
        }
        return ans;
Leave a Comment