java simple solution


  • 0
    J

    slightly longer than O(n). 8ms solution.

    int[] charTable = new int[128];

    	 for(int i=0; i<s.length();i++){
    		charTable[(int)(s.charAt(i))]+=1;
    		charTable[(int)(t.charAt(i))]-=1;
           
        }
        
    	 charTable[(int)(t.charAt(t.length()-1))]-=1;
    
    
    	 for(int i = 0; i<128; i++){
    		 if(charTable[i]!=0){
    			 return (char)i;
    		 }
    	 }
    	 return ' ';
        
    }

  • 0
    S
        int[] hash = new int[26];
        
        for( int i = 0; i < hash.length; i++ )
        	hash[i] = 0;
    
        for( int i = 0; i < t.length(); i++ )
        	hash[ t.charAt(i) - 'a' ]++;
        
        for( int i = 0; i < s.length(); i++ )
        	hash[ t.charAt(i) - 'a' ]--;
    
        for( int i = 0; i < hash.length; i++ )
        	if( hash[i] > 0 )
    
        		return (char)('a' + i);
        
        return 'a';
    

    hello, I am a new java user.I used to use C++. Could you help me to correct my code?


  • 0
    L

    line 10 you should change t to s


Log in to reply
 

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