Why my java solution time limit exceed?


  • 0
    V

    import java.util.Hashtable;

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

       if(s.length() == 0){
    		return 0;
    	}
    	
    	int count = 0;
    	int temp = 0;
    	int max = 0;
    	char[] chars = s.toCharArray();
    	Hashtable<Character, Integer> table = new Hashtable<Character, Integer>();
    	
    	for(int i = 0; i < s.length(); i++){
    		if(table.containsKey(chars[i])){
    			max = Math.max(max, i - temp);
    			temp = table.get(chars[i]) + 1;
    			i = temp;
    			table.clear();
    			count = 0;
    		}
    		
    		table.put(chars[i], i);
    		count++;
    	}
    	
    	return Math.max(max, count);
    	
    }
    

    }


Log in to reply
 

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