Hints

Java

  • Calculate hamming distance by comparing bits: Integer.bitCount(x ^ y)
  • Utilize split() to count appearance of characters, add blank space in the beginning and end of the String first
  • string.replaceAll("regular expression","")
  • count prime numbers: Sieve of Eratosthees
public int countPrimes_1(int n)
{
	boolean[] isPrime = new boolean[n];
	for (int i = 2; i < n; i++)
		isPrime[i] = true;
	for (int i = 2; i * i < n; i++)
	{
		if (!isPrime[i])
			continue;
		for (int j = i * i; j < n; j += i)
			isPrime[j] = false;
	}
	int count = 0;
	for (int i = 0; i < n; i++)
		if (isPrime[i])
			count++;
	return count;
}
  • always consider boundary or overflow numbers
  • when we want to log the appearance of some characters, we can initialize an integer array with size 256, and since each char refers to a number, we can use char as index, and value as times of apperance.
int[] chars = new int[256];
for(char c : s.toCharArray())
{
	chars[c]++;
}