// set must be a case-insensitive set // set must not contain empty string static int substringsScoreIC_optimized1(S s, Set set) { int maxLength = lengthOfLongestString(set); int n = l(s); Set seen = ciSet(); for i to n: { int k = min(i+maxLength, n); for (int j = i+1; j <= k; j++) { S sub = substring(s, i, j); if (set.contains(sub)) seen.add(sub); } } ret l(seen); }