Simple java code using set but wrong,WHY?


  • 0

    Here is my code:

    public int lengthOfLongestSubstring(String s) {
        TreeSet noRepeat = new TreeSet();
        for(int i = 0; i < s.length(); i++){
            noRepeat.add(""+s.charAt(i));
        }
        return noRepeat.size();
    }
    
    • Input:
      "pwwkew"
    • Output:
      4
    • Expected:
      3

    i think my code is right and is very simple


  • 0

    Because your code has nothing to do with the problem.

    My guess is you're yet another person who doesn't know the difference between substring and subsequence...


  • 0
    P

    Because your code counts the number of different characters in this string.
    The answer is because there are 'p','w','k' and 'e'.


Log in to reply
 

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