Why my Java codes cannot handle strings with "\" ?


  • 0
    Y

    //My code can run most test cases correctly, except those with "" (e.g. ""\qwertq"").
    //I am really confused about it, could you help me? Thanks!

        public class Solution {
        public int lengthOfLongestSubstring(String s) {
            if(s.length() == 0)
                return s.length();
            int max = 1;
            for(int i = 0; (i+1)<s.length(); i++){
                int j = i+1;
                int count = 1;
               
                while(j < s.length() && !s.substring(i,i+1).equals(s.substring(j,j+1)) ){
                    boolean dup = false; 
                    int tempI = i+1;
                    while(tempI < j){
                        if(s.substring(tempI, tempI+1).equals(s.substring(j,j+1))){
                            dup = true;
                            break;
                        }
                        tempI ++;
                    }
                    if(dup == true)
                        break;
                    count ++;
                    j ++;
                }
                if(count > max)
                    max = count;
            }
            return max;
        } 
    }

Log in to reply
 

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