Skip to content

Instantly share code, notes, and snippets.

@janvichhabra
Last active December 31, 2021 12:20
Show Gist options
  • Save janvichhabra/a3b0a029db3c8b30f24b3caaa7ca0e99 to your computer and use it in GitHub Desktop.
Save janvichhabra/a3b0a029db3c8b30f24b3caaa7ca0e99 to your computer and use it in GitHub Desktop.
Valid Anagram
1. What is anagram?
An anagram is a string that can be formed by rearranging the characters of a different string using all the original characters exactly once.
2. What is valid anagram?
An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other.
3. How do you check if a word is an anagram?
Two words are anagrams of each other if they contain the same number of characters and the same characters. You should only need to sort the characters in lexicographic order, and determine if all the characters in one string are equal to and in the same order as all of the characters in the other string.
4. Can anagrams have different length?
If we could sort both the strings, we will have the characters organized in an order which will be identical if both the strings are anagram to each other. So, strings with different lengths would never be an anagram.
5. Can we iterate HashMap?
There is a numerous number of ways to iterate over HashMap of which 3 are listed as below: Iterate through a HashMap EntrySet using Iterators. Iterate through HashMap KeySet using Iterator. Iterate HashMap using for-each loop.
1. Parse first string, and populate the HashMap whose key is the character and value is the number of occurences of that character in the first string.
2. Now parse the the second string, and return false if we encounter a character that is not in the map (meaning this character is not in the first string)
3. As we parse the second string, decrement the occurence values in the HashMap for each character.
4. Finally lookup the occurence counts for each character in the HashMap and return a false if you encounter a non-zero value.
1. "abll" and "ball" are anagrams.
A. True
B. False
Ans: A. True
2. "tall" and "all" are anagrams.
A. True
B. False
Ans: B. False
3. In iteration of second array, we can simply ______ count of found characters
A. decrement
B. increment
C. equal
D. none of te above
Ans: A. decrement
4. What is the average time complexity of this problem?
a) O(n*n)
b) O(n)
c) O(1)
d) None of the above
Ans: b
5. What is the average space complexity of this problem?
a) O(n*n)
b) O(1)
c) O(n)
d) None of the above
Ans: c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment