AfterAcademy Tech
•
11 Jun 2020

Difficulty: Medium
Asked in: Google, Amazon, Microsoft
Problem Description
Given two strings s and t, Write a program to check whether the two strings are an anagram of each other or not.
Problem Note:
s is an anagram of t if the characters of s can be rearranged to form t.Example 1
Input: s = “afteracademy” , t = “academyafter”
Output: 1
Example 2
Input: s = “mindorks”, t = “mindrocks”
Output: 0
We will be discussing two solutions for this interview problem
You may try this problem here
If you rearrange the letters of s into t, then that would be anagram to each other. 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.
Solution steps
0, else step 2s and t01Pseudo Code
bool isAnagram(String s, String t) {
if (not s.length() == t.length()) {
return 0
}
char[] str1 = s.toCharArray()
char[] str2 = t.toCharArray()
sort(str1)
sort(str2)
for(i = 0 to i < s.length()){
if(str1[i] != str2[i]){
return 0
}
}
return 1
}
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Note: In most of the programming language, strings are immutable and thus requires conversion into character array to manipulate it.
Critical Ideas to Think
When we sorted the strings and started matching in the previous approach, we were only checking their occurrences of each character by grouping them together.
So, we could do that by storing the number of occurrences of each character in an array where each index represents the ASCII of the respective character. In this way, we don’t have to reorder the strings, we could directly increment the value at the index by one on each occurrence and compare the second string with it. Instead of using an array whose each index represent the ASCII of character, we could also use a hashmap, whose key is the character itself and the value is their number of occurrence in the first string which works exactly the same as of array.
Solution Steps
0 if the length of the strings are unequal else step 2counter array of size depending on the possible number of different characters and initialize it with 0s increment the counter[i] (where i is the ASCII of the character) and for each character of t decrement the counter[i]counter contains 0 in the end, return 1 else return 0Pseudo Code
bool isAnagram(String s, String t) {
if (not s.length() == t.length()) {
return 0
}
// assuming the characters of the strings be lower case only
int counter[26]
for (int i = 0 to i < s.length()) {
counter[s[i] - 97] = counter[s[i] - 97] + 1
counter[t[i] - 97] = counter[t[i] - 97] - 1
}
for (i=0 to i < s.length()) {
if (counter[i] != 0) {
return 0
}
}
return 1
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1), The space utilized by the counter array is constant.
Critical Ideas to Think
false whenever counter[i] != 0 is true?s[i] with 97?
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given two strings S1 and S2 of size m and n respectively, you need to check whether the two strings are an anagram of each other or not. S1 is an anagram of S2 if the characters of S1 can be rearranged to form S2.

AfterAcademy Tech
Given two sequences pushed and popped with distinct values, write a program to return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.

AfterAcademy Tech
In this blog, we will learn various types of constraints that can be applied on a table. These constraints can be used to validate the data present in the table. We will learn about NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT
