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 king decided to let a prisoner try to escape the prison with his life. The king placed 2 marbles in a jar that was glued to a table. One of the marbles was supposed to be black, and one was supposed to be blue. If the prisoner could pick the blue marble, he would escape the prison with his life. If he picked the black marble, he would be executed. However, the king was very mean, and he wickedly placed 2 black marbles in the jars and no blue marbles. The prisoner witnessed the king only putting 2 black marbles in the jars. If the jar was not see-through and the jar was glued to the table and that the prisoner was mute so he could not say anything, how did he escape with his life?
The prisoner grabbed one of the marbles from the jar and concealed it in his hand. He then swallowed it, and picked up the other marble and showed everyone. The marble was black, and since the other marble was swallowed, it was assumed to be the blue one. So the mean king had to set him free.
Bruce is an inmate at a large prison, and like most of the other prisoners, he smokes cigarettes. During his time in the prison, Bruce finds that if he has 3 cigarette butts, he can cram them together and turn them into 1 full cigarette. Whenever he smokes a cigarette, it turns into a cigarette butt.
One day, Bruce is in his cell talking to one of his cellmates, Steve.
"I really want to smoke 5 cigarettes today, but all I have are these 10 cigarette butts," Bruce tells Steve. "I'm not sure that will be enough."
"Why don't you borrow some of Tom's cigarette butts?" asks Steve, pointing over to a small pile of cigarette butts on the bed of their third cellmate, Tom, who is out for the day on a community service project.
"I can't," Bruce says. "Tom always counts exactly how many cigarette butts are in his pile, and he'd probably kill me if he noticed that I had taken any."
However, after thinking for a while, Bruce figures out a way that he can smoke 5 cigarettes without angering Tom. What is his plan?
Bruce takes 9 of his 10 cigarette butts and turns them into 3 cigarettes total (remember, 3 cigarette butts can be turned into 1 cigarette). He smokes all three of these, and now he has 4 cigarette butts.
He then turns 3 of the 4 cigarette butts into another cigarette and smokes it. He has now smoked 4 cigarettes and has 2 cigarette butts.
For the final step, he goes and borrows one of Tom's cigarette butts. With this cigarette butt plus the 2 he already has, he is able to make his 5th cigarette to smoke. After smoking it, he is left with 1 cigarette butt, which he puts back in Tom's pile so that Tom won't find anything missing.
Two Japanese people who have never seen each other meet at the New York Japanese Embassy. They decide to have drinks together at a nearby bar. One of them is the father of the other one's son. How is this possible?
The Japanese are husband and wife and both blind since birth.
Note: This riddle must be done IN YOUR HEAD ONLY and NOT using paper and a pen.
Take 1000 and add 40 to it.
Now add another 1000.
Now add 30.
Now add 20.
Now add another 1000.
Now add 10.
What is the total?
The answer is 4100, check it out on a calculator. Did you think it was 5000? Most people add the 100 as 1000 by mistake.