import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; class main { public static void main(final String[] args) throws Exception { TreeSet set = new TreeSet(); set.add("pot"); set.add("potash"); System.out.println(containsPrefixOf(set, "pot")); } static boolean containsPrefixOf(TreeSet set, String s) { if (set == null || s == null) return false; while (true) { String key = set.floor(s); if (key == null) return false; // s is in front of whole set => no prefix in there int n = lengthOfCommonPrefix(key, s); if (n == key.length()) return true; // found! s = s.substring(0, n); // shorten and try again } } static int lengthOfCommonPrefix(String a, String b) { int i = 0, n = Math.min(a.length(), b.length()); while (i < n && a.charAt(i) == b.charAt(i)) ++i; return i; }}