For the string qopubjguxhxdipfzwswybgfylqvjzhar my code is getting output as 9 but expected is 12.I m not able to find.


  • 0
    A
    public int lengthOfLongestSubstring(String s) {
             //initialise the character array to story the string characters
    		char[] c = s.toCharArray();
    		int longest = 0;
    
    		String temp = "";
    		for (int i = 0; i < s.length(); i++) {
    			int ind = temp.indexOf(c[i]);
    			if (ind > -1) {
    				if (temp.length() > longest) {
                       //getting the longest substring length 
    					longest = temp.length();
    				}
                    //if the substring is already present empty the built substring and start from the current character again
    				temp = temp.substring(ind + 1) + s.charAt(i);
    			} else {
                        //otherwise continue
    				temp = temp + s.charAt(i);
    			}
    		}
    		return longest;
    	}

  • 0
    S

    Hi @anirbanguha, Thanks for your posting. Have you read the FAQ of this discuss? We encourage high quality posts. Please update your post, format your code correctly, select all code and click {} button and try to add comments in it. Thanks.


  • 0
    A

    Hi @Shangrila.Thanks for your comment.I hope i am ok this time with the code.Can you please get me the answer of this question.


  • 0
    S

    Thanks for your updating~ I will add my answer.


  • 0
    S

    From your code if (ind > -1), it tells that you only update longest when it is true. You just forget the last substring, which contains all characters of last part of s, like bcd of abbcd.

    So I just guess test case abbcd would also fail your code, not verify it.


  • 0
    A

    Hi @Shangrila.Thanks a lot for your answer.I was able to find the bug.


  • 0
    J

    For the string qopubjguxhxdipfzwswybgfylqvjzhar,the longest substring without repeating ends at the last character. But in your code, it can't count the length of the substring. You need add
    if (temp.length() > longest) {
    longest = temp.length();
    }
    below the line of temp = temp + s.charAt(i); in the else


  • 0
    C

    check its working fine..its also accepted..

    public class Solution {
    public int lengthOfLongestSubstring(String s) {

       int len = 0;
       String result = "";
       String sb = "";
       int count=0;
    
    	for (int i = 0; i < s.length(); i++) {
    
    		if (sb.indexOf(s.charAt(i)) == -1) {
    			sb += s.charAt(i);
    		} else {
    					
    			if(len<sb.length()){
    				len=sb.length();
    				result=sb;
    			}
    			sb = "";
    		   i=i-(i-count++);
    
    		}
    	}
    
    if(len<sb.length())
    		return sb.length();	
    else
    			return len;
    }
    

    }


  • 0
    A

    As Shangrila said, the return statement should be

    return longest>temp.size()?longest:temp.size();

  • 0
    S

    I think it might be better if we can avoid the functions like charAt. It goes complicated when you are asked for complexities.


Log in to reply
 

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