TreeSet is not defined error

  • 1
    I tried to use TreeSet for this problem. But compiler reported the below error. Please help me why this error occurred. Please let me know if you need any extra information.
    Code :
    public class Solution {
        public boolean wordBreak(String s, Set<String> dict) {
            boolean flag = false;
            String temp = null;
            Iterator it ;
            TreeSet<String> tsdict = new TreeSet<String>(new Comparator(){
               public int compare(Object o1, Object o2) {
                   String t1 = (String)o1;
                   String t2 = (String)o2;
                   return (-1)*t1.compareTo(t2);
            while( !s.equals("")) {
                it = tsdict.iterator();
                flag = false;
                    temp = (String);
                        flag = true;
                if(flag == false) return false;
                s = s.substring(temp.length());
            return true;
    Submission Result: Compile Error
    Line 6: cannot find symbol: class TreeSet

  • 0

    Could you please format your code by selecting your code and press the {} button?

  • 0

    By adding the following line:

    import java.util.TreeSet;

    Your code compiles fine.

    Anyway, all Java code should include TreeSet by default now, so you don't have to add the import line manually from now on.

  • 0

    Thanks. Worked.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.