Hints
Java
- Calculate hamming distance by comparing bits:
Integer.bitCount(x ^ y) - Utilize
split()to count appearance of characters, addblank spacein the beginning and end of theStringfirst string.replaceAll("regular expression","")- Count prime numbers: Sieve of Eratosthenes
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, initialize an integer array with size 256 — since each
charmaps to a number, usecharas index and value as count:
int[] chars = new int[256];
for (char c : s.toCharArray())
{
chars[c]++;
}