IndexOutofBound for length of longest unique substring


  • 0
    S

    The input for which I am getting IndexOutOfBound is "". Where as I am specifically handling that case.

        public static int _lengthOfLongestSubstring(String s) {
            if (s == null) {
                return 0;
            }
    
            if (s == "") {
                return 1;
            }
    
            if (s.length() == 1) {
                return 1;
            }
    
            HashMap<Character, Boolean> cbMap = new HashMap<Character, Boolean>();
            int i = 0;
            while (s.length() > i) {
                if (!cbMap.containsKey(s.charAt(i))) {
                    cbMap.put(s.charAt(i), true);
                    i++;
                } else {
                    break;
                }
            }
            int length = (s != "") ? _lengthOfLongestSubstring(s.substring(1, s.length())) : 1;
    
            if (length > i) {
                return length;
            } else {
                return i;
            }
    
        }
    

Log in to reply
 

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