Why the input "dvdf" expected 3?


  • 2
    Z

    I think the output should be 2, "dv". Perhaps the test case is wrong? my c# code as follow:

    public class Solution {
    public int LengthOfLongestSubstring(string s) {
        if(string.IsNullOrEmpty(s))
            return 0;
    
        BitArray ascii = new BitArray(128);        
        int idx = 0, max = 0, count = 0;
        
        foreach(char c in s){
            idx = (int)c;
            if(ascii[idx]==true){
                max = Math.Max(max, count);
                ascii.SetAll(false);
                count = 0;
            }
            ascii[idx]=true;
            count+=1;
        }
        
        return Math.Max(max, count);; 
    }
    

    }


  • 0
    S

    The problem is worded pretty ambiguously, but I interpret it to mean the longest chain of unique characters in the whole string (not just the beginning of the string), so it's 3 because of "vdf"


  • 2
    E

    It is the longest substring without repeating characters. Since vdf is a substring and does not have repeating characters, it satisfies the condition.


Log in to reply
 

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