TreeSet is not defined error


  • 1
    P
    Hi,
    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);
               }
            });
            tsdict.addAll(dict);
            while( !s.equals("")) {
                it = tsdict.iterator();
                flag = false;
                while(it.hasNext()){
                    temp = (String)it.next();
                    if(s.startsWith(temp)){
                        flag = true;
                        break;
                    }
                }
                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
    P

    Thanks. Worked.


Log in to reply
 

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