Two words are anagrams if and only if they contain the exact same letters with the exact same frequency (for example, "name" and "mean" are anagrams, but "red" and "deer" are not).
Given two strings S1 and S2, which each only contain the lowercase letters a through z, write a program to determine if S1 and S2 are anagrams. The program must have a running time of O(n + m), where n and m are the lengths of S1 and S2, respectively, and it must have O(1) (constant) space usage.
First create an array A of length 26, representing the counts of each letter of the alphabet, with each value initialized to 0. Iterate through each character in S1 and add 1 to the corresponding entry in A. Once this iteration is complete, A will contain the counts for the letters in S1. Then, iterate through each character in S2, and subtract 1 from each corresponding entry in A. Now, if the each entry in A is 0, then S1 and S2 are anagrams; otherwise, S1 and S2 aren't anagrams.
Here is pseudocode for the procedure that was described:
def areAnagrams(S1, S2)
A = new Array(26)
for each character in S1
arrayIndex = mapCharacterToNumber(character) //maps "a" to 0, "b" to 1, "c" to 2, etc...
A[arrayIndex] += 1
for each character in S2
arrayIndex = mapCharacterToNumber(character)
A[arrayIndex] -= 1
for (i = 0; i < 26; i++)
if A[i] != 0
A train leaves from Halifax, Nova Scotia heading towards Vancouver, British Columbia at 120 km/h. Three hours later, a train leaves Vancouver heading towards Halifax at 180 km/h. Assume there's exactly 6000 kilometers between Vancouver and Halifax. When they meet, which train is closer to Halifax?
Both trains would be at the same spot when they meet therefore they are both equally close to Halifax.
How can you divide a pizza into 8 equal slices using only 3 straight cuts?
Cut 1: Cut the pizza straight down the middle into two halves.
Cut 2: Keeping the two halves in the place, cut the pizza straight down the middle at right angles to the first cut (you will be left with 4 equal quarters)
Cut 3: Pile the 4 quarters on top of each other and cut through the middle of the pile. You will be left with 8 equal slices.
In a supermarket, the first 25 customers of the day purchased an average of two items each.
After a further 15 customers, the average number of items purchased by each customer rose to eight.
What was the average number of items purchased by the last 15 customers only?