NetApp Interview Question
Software Engineer / Developersstatic int IsAnagrams(string str1, string str2)
{
str1 = str1.ToLower();
str2 = str2.ToLower();
if (str1.Length != str2.Length)
{
Console.WriteLine("Strings are not anagrams of each other the lengths are not equal");
return 0;
}
Hashtable ht1 = new Hashtable();
for (int i = 0; i < str1.Length; i++)
{
ht1.Add(i,str1[i]);
}
Hashtable ht2 = new Hashtable();
for (int i = 0; i < str2.Length; i++)
{
ht2.Add(i, str2[i]);
}
int countsareequal = 0;
foreach (char c in str1)
{
if (ht1[c] == ht2[c])
{
countsareequal += 1;
}
}
if (countsareequal == str1.Length)
{
Console.WriteLine("two strings are anagrams");
return 1;
}
else
{
Console.WriteLine("two strings are anagrams");
return 0;
}
}
Space: O(1)
Time: O(n)
#include <stdio.h>
int main(int argc, char *argv[])
{
char map[255] = { 0 }, *answ = "YES";
int i,len1 = strlen(argv[1]), len2 = strlen(argv[2]);
if(len1 == len2) {
for(i = 0; i < len1;i++)
map[argv[1][i]]++;
for(i=0;i < len2;i++)
map[argv[2][i]]--;
for(i = 0; i < sizeof(map)/sizeof(map[0]); i++)
if(map[i] != 0) {
answ = "NO";
break;
}
}
else
answ = "NO";
printf("%s\n",answ);
return (0);
}
void anagram(char *d, char *c, int length)
{
int i=0;
while(length--)
{d[i]=c[length];
i++;}
d[i]='\0';
}
Map<String, List<String>> anagrammsMap = ....
- Maxim Stepanenko March 09, 2011public void addAnagramm(String str){
char[] strArr = str.toCharArray();
Arrays.sort( strArr );
String key = new String( strArr );
List<String> anagramms = anagrammsMap.get( key );
if( anagramms == null ){
anagrammsMap.put(key, anagramms);
}
anagramms.add( str );
}