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++){
        return noRepeat.size();
    • Input:
    • Output:
    • Expected:

    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

    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.