Java O(n) Solution : 65ms


  • 0
    A

    Here is my Java Solution :

    983 / 983 test cases passed.
    Status: Accepted
    Runtime: 65 ms

    	public static int uniqueSubstring(String str) {
    		String longestUniqueSubstring = new String();
    		String uniqueSubstring = new String();
    		for (int i = 0; i < str.length(); i++) {
    			String x = String.valueOf(str.charAt(i));
    			if (uniqueSubstring.contains(x)) {
    				uniqueSubstring = uniqueSubstring.substring(
    						uniqueSubstring.lastIndexOf(x) + 1,
    						uniqueSubstring.length());
    			}
    			uniqueSubstring += x;
    			if (uniqueSubstring.length() > longestUniqueSubstring.length())
    				longestUniqueSubstring = uniqueSubstring;
    		}
    		return longestUniqueSubstring.length();
    	}
    
    

Log in to reply
 

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