Can someone help me to figure out what's the problem with my code. Any help will be appreciated


  • 0
    A
    public class LongestSub {
    	public int lengthOfLongestSubstring(String s){
    		int counter=0;
    		int length=0;
    		String substring;
    		outer:for (int i=0;i<s.length();i++){
    			
    				for(int j=i+1;j<s.length();j++){
    				
    					substring=s.substring(i,j);
    					counter=substring.length();
    					length=(length<counter?counter:length);
    					char[] sub=substring.toCharArray();
    					char[] ch=s.substring(j,j+1).toCharArray();
    						for(int n=0;n<substring.length();n++){
    				
    							if(sub[n]==ch[0]){
    								continue outer;
    							}
    						}
    				}
    		}
    		return length;
    	}
    
    }

  • 0
    S

    I think you are first seeing if the substring is longer than the current longest and THEN checking if the substring doesn't have duplicates.

    It would help if you'd given an example of a failed test case.


Log in to reply
 

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