Java 23ms solutions


  • 0
    L
    public static boolean canConstruct(String ransomNote, String magazine) {
    	byte[] check = new byte[magazine.length()]; 
    	for (int i = 0; i < ransomNote.length(); i++) {
    		char ch = ransomNote.charAt(i);
    		boolean found = false;
    		for (int j = 0; j < magazine.length(); j++) {
    			if (check[j] == 0 && ch == magazine.charAt(j)) {
    				check[j]++;
    				found = true;
    				break;
    			}
    		}
    		if(!found) {
    			return false;
    		}
    	}
    	return true;
    }

Log in to reply
 

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