Time Limit Exceeededddddd !!!! Helpppppp


  • 0
    T

    here my code, please help me :((((((

    public class Solution {
        public int lengthOfLongestSubstring(String str) {
                 
            boolean flag = true;
            String temp = "";
            int maxLength = 0;
            String longestStr = "";
            for(int i=0;i<str.length();i++){
                flag = true;
                temp = Character.toString(str.charAt(i));
                for(int j=i+1;j<str.length();j++){
                    if(flag){
                        if(str.charAt(i) != str.charAt(j)){
                            if(!temp.contains(Character.toString(str.charAt(j)))){
                                temp += Character.toString(str.charAt(j));
                            }else{
                                flag = false;
                            }
                        }else if(str.charAt(i) == str.charAt(j)){
                            flag = false;
                        }
                    }
                }
    
                if(temp.length()>maxLength){
                    maxLength = temp.length();
                    longestStr = temp;
                }
    
            }
             return maxLength;
        }
    }
    

Log in to reply
 

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