Use HashSet to determine the result
public class SubsetChecker {
public boolean isSubset(char[] first, char[] second) {
Set<Character> set = generateCharacterSet(first);
return containsAll(set, second);
public class SingletonSample{ | |
private static SingletonSample instance= null; | |
private static Object mutex= new Object(); | |
private String value; | |
private SingletonSample(){} | |
public static SingletonSample getInstance(){ |
import java.util.HashMap; | |
import java.util.Map; | |
public class SimilarNumberCounter { | |
private static Map<Integer, Integer> digitCounts = new HashMap<>(); | |
private static void initializeDigitCounts() { | |
for (int i = 0; i <= 9; i++) { | |
digitCounts.put(i, 0); |
import java.math.BigInteger; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
public class CombinationCounter { | |
private static List<BigInteger> generateCombinationLimitedBy(int index) { | |
List<BigInteger> sequence = new ArrayList<>(); |
package edu.self.binding; | |
import android.databinding.BindingAdapter; | |
import android.widget.ImageView; | |
import com.bumptech.glide.Glide; | |
public class CustomSetter { |
package com.example.retrofit; | |
import org.junit.Test; | |
import java.io.ByteArrayOutputStream; | |
import java.io.PrintStream; | |
import static junit.framework.TestCase.assertTrue; | |
// 一般來說,只要把名稱取為 ContributorAppTest 即可 |
package self.edu.observable; | |
import io.reactivex.Observable; | |
import io.reactivex.Scheduler; | |
import io.reactivex.observers.TestObserver; | |
import io.reactivex.schedulers.Schedulers; | |
import io.reactivex.schedulers.TestScheduler; | |
import org.junit.Assert; | |
import org.junit.Test; |
Let the size of the first array is N and the size of the second array is M. The big O of the time complexity of this algorithm is O(N + M)
In the beginning, we put all elements in the first array into a HashSet object, the time complexity is O(N) because we walk throught all elements, and saving a element into a hash set only costs a constant time.
In order to determine if the second array is a subset of the first array, we walk through all elements in the second array, and check if each element has already been in the hash set. Because finding a element in a hash set also costs a constant time, the time complexity is O(M)
After executing the above steps, we can know if the second array is a subset of the first array. The total time complexity is O(M + N)