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
Without looking at a calendar, within a minute name a boys name using 5 consecutive first letters of 5 consecutive months.
JASON - July August September October November.animalcleanfunnyshort
What animal do you think is the best at baseball?
Dracula’s going to the bank to keep his money, he seems a little more pale than usual, which bank did he go to?
A Blood Bank.cleanlogicshort
Re-arrange the letters, O O U S W T D N E J R to spell just one word.
"Just one word".logicshort
What country is hidden in the paragraph below?
As defendants, we deny all involvement in the unscrupulous dealings which have come to light in the recent government investigation.
What has six faces, but does not wear makeup. It also has twenty-one eyes, but cannot see?
A die (dice).funny
A man came to visit at a convent while the superior mother was out of town. He left before she returned, and was careful to leave nothing behind. The nuns said nothing about his visit, so how did the superior mother figure out that a man had been there?
Because a toilet seat was up.logic
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.short
If I drink, I die. If i eat, I am fine. What am I?