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)
A.initializeValues(0)
for each character in S1
arrayIndex = mapCharacterToNumber(character) //maps "a" to 0, "b" to 1, "c" to 2, etc...
A[arrayIndex] += 1
end
for each character in S2
arrayIndex = mapCharacterToNumber(character)
A[arrayIndex] -= 1
end
for (i = 0; i < 26; i++)
if A[i] != 0
return false
end
end
return true
end
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.
In a new Engineering Hostels they have 100 rooms. Ankit Garg was hired to paint the numbers 1 to 100 on the doors. How many times will Ankit have to paint the number eight ?
A deliveryman comes to a house to drop off a package. He asks the woman who lives there how many children she has.
"Three," she says. "And I bet you can't guess their ages."
"Ok, give me a hint," the deliveryman says.
"Well, if you multiply their ages together, you get 36," she says. "And if you add their ages together, the sum is equal to our house number."
The deliveryman looks at the house number nailed to the front of her house. "I need another hint," he says.
The woman thinks for a moment. "My youngest son will have a lot to learn from his older brothers," she says.
The deliveryman's eyes light up and he tells her the ages of her three children. What are their ages?
Their ages are 1, 6, and 6. We can figure this out as follows:
Given that their ages multiply out to 36, the possible ages for the children are:
1, 1, 36 (sum = 38)
1, 2, 18 (sum = 21)
1, 3, 12 (sum = 16)
1, 4, 9 (sum = 14)
1, 6, 6 (sum = 13)
2, 2, 9 (sum = 13)
2, 3, 6 (sum = 11)
3, 3, 4 (sum = 10)
When the woman tells the deliveryman that the children's ages add up to her street number, he still doesn't know their ages. The only way this could happen is that there is more than one possible way for the children's ages to add up to the number on the house (or else he would have known their ages when he looked at the house number). Looking back at the possible values for the children's ages, you can see that there is only one situation in which there are multiple possible values for the children's ages that add up to the same sum, and that is if their ages are either 1, 6, and 6 (sums up to 13), or 2, 2, and 9 (also sums up to 13). So these are now the only possible values for their ages.
When the woman then tells him that her youngest son has two older brothers (who we can tell are clearly a number of years older), the only possible situation is that their ages are 1, 6, and 6.
You have two sand hourglasses, one that measures exactly 4 minutes and one that measures exactly 7 minutes. You need to measure out exactly 2 minutes to boil an egg. Using only these two hourglasses, how can you measure out exactly 2 minutes to boil your egg?
Flip over both hourglasses at the same time.
1. After 4 minutes, the 4-minute hourglass will be done, and there will be 3 minutes left in the 7-minute hourglass. Immediately flip the 4-minute hourglass over again.
2. After 3 more minutes, the 7-minute hourglass will be done, and there will be exactly 1 minute left in the 4-minute hourglass. Immediately flip the 7-minute hourglass over again.
3. After 1 more minute, the 4-minute hourglass will be done again, and there will be exactly 6 minutes left in the 7-minute hourglass. Immediately flip over the 4-minute hourglass.
4. After 4 more minutes, the 4-minute hourglass will be done again, and there will be exactly 2 minutes left in the 7-minute hourglass. At this point, put your egg in the boiling water. When the 7-minute hourglass is done, it will have been exactly 2 more minutes, and your egg will have boiled just right.
Or after step 2 just flip 7-minute hourglass for second minute.