Help please! How can I optimize?


  • 0
    M
    class Solution {
        private int maxLen = Integer.MIN_VALUE;
        public int lengthOfLongestSubstring(String s) {
            if(s == null || s.length() < 1){
                return 0;
            }
            maxSub(s);
            return maxLen;
        }
        
        private void maxSub(String s){
            if(s == null || s.length() < 1){
                return;
            }
            if(s.length() == 1){
                maxLen = Math.max(1, maxLen);
                return;
            }
            Map<Character, Boolean> map = new HashMap<>();
            for(int i=0;i<s.length();i++){
                if(map.containsKey(s.charAt(i))){
                    break;
                }
                map.put(s.charAt(i), true);
                maxLen = Math.max(i+1, maxLen);
            }
            maxSub(s.substring(1, s.length()));
            maxSub(s.substring(0, s.length()-1));
        }
    }
    

Log in to reply
 

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