30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied.
Book (308 Pages):
  • 150 Programming Interview Questions and Solutions
  • Five Proven Approaches to Solving Tough Algorithm Questions
  • Ten Mistakes Candidates Make -- And How to Avoid Them
  • Steps to Prepare for Behavioral and Technical Questions
  • Interview War Stories: A View from the Interviewer's Side
  • Book Preview and More Info

Video (One Hour):
  • Watch CareerCup's founder perform a totally unscripted technical interview of a candidate.
  • Overview of what interviewers look for in software engineering jobs.
  • Technical questions with white-boarding coding where the candidate does well - and struggles.
  • Interviewer reviews with what went well and poorly.
  • Video Preview and More Info

Resume Review (24 - 48hr)
  • Get your resume reviewed by a trained engineering interviewer. More Info
All Products / Services


Express Prep Package (Book)
$29.99 for e-book | $32.45 for paperback
Buy E-Book Buy on Amazon


Standard Prep Package (E-Book & Video)
Emailed Instantly | $39.99
Buy Standard

Elite Prep Package (E-Book, Video & Resume)
Emailed Instantly | $99.99
Buy Elite
 



Give an algorithm to find whether 2 given strings are ANAGRAMS or not. Write test cases.

11


Anonymous on November 14, 2009 |Edit | Edit

sort the 2 words and compare if both strings are same then both are anagrams

Anonymous on November 15, 2009 |Edit | Edit

Another approach with O(n) space and time is use a hash to keep track of count of each char in string1, While parsing string2, decrement the count of the char in hash table(delete when count becomes zero). If hash table is empty at end, its anagram.

Amol Agarwal on June 17, 2010 |Edit | Edit

Also unlike array we do not need to allocate space in advance for any possible character. If there are only 50 different characters in a string. Size of HashMap will be 50 only after iterating first string.

kunalgaind on November 15, 2009 |Edit | Edit

I will use extra space to solve this question, as sorting is a costly affair.

Regarding extra space, we only need to use memory for 256 character, not of O(n) if we know that string is made up of only ASCII characters. We can do more optimization on space, if we can put more restriction on our data type like if string will consist only of lower and upper character than I need space for only 50 characters......

Anonymous on November 17, 2009 |Edit | Edit

From "http://www.careercup.com/question?id=18382"

solution was provided by "nirmalr" (modified slightly)

O(n) algorithm to check anagram :
+++++++++++++++++++++++++++++++++

variant of counting sort can be used here as the range is known.

algorithm :

input : string1, string2 (to be checked)

step 1: allocate one integer array c[26]
step 2: initialize elements of array c to zero

step 3 : for each character in string1
increment corresponding element in c by one (ex : for 'a', c[0]++)

step 4 : for each character in string2
decrement corresponding element in c by one (ex : for 'a', c[0]--)

step 5 : if "c" contains all zero then string1 & 2 are anagrams.

for optimization, step 3 and 4 can be merged together as they are independent activities.

Lahori on December 10, 2009 |Edit | Edit

AWESOME, thanks buddy!

Anonymous on January 04, 2010 |Edit | Edit

wat awesome.. this is what hash map is doing

Anonymous on November 18, 2009 |Edit | Edit

nirmalr's algorithm assumes there is only alphabet characters. What about numbers and special characters? :)

Anonymous on November 18, 2009 |Edit | Edit

bool isAnagram(string s1,string s2)
{
return s1.sort()==s2.sort();
}

sumit saxena on June 12, 2010 |Edit | Edit

This is the standard algorithm to find anagrams.
The logic is to find signatures of the string (in this case its alphabatical order) and then compare the signatures.
Anagrams have the same signatures.

pankaj.dec21 on April 16, 2010 |Edit | Edit

i have a different approach using prime numbers.
all u need to do is maintain an array of first 26 prime numbers.

then for input string1 find its unique product value:
product_string1=1
for(i=0 to strlen(string1)) product_string1*=a[string[i]-'a']

if(strlen(string2)==strlen(string2)){
then find the product for string 2 in similar fashion
product_string2=1
for(i=0 to strlen(string2)) product_string2*=a[string[2]-'a']
if(product_string1==product_string2) string is anagram of string 1
}

Time complexity: Find the first 26 prime numbers, this will be done once only.
After that for every input it can said in o(n) time whether it is anagram or not where n is the string size. and space complexity is only int array of 26 to store first 26 prime numbers.

Add a Text Comment | Add Runnable Code
Name:
Comment:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.








Amazon (1033)Bloomberg LP (403)Qualcomm (117)Adobe (88)Goldman Sachs (78)NetApp (49)IBM (43)Morgan Stanley (33)CapitalIQ (30)Sophos (25)Achieve Internet (23)Electronic Arts (19)Motorola (18)Research In Motion (17)Flipkart (16)
Microsoft (867)Google (141)NVIDIA (98)Yahoo (82)Epic Systems (69)Expedia (47)VMWare Inc (43)Apple (32)Cisco Systems (28)Facebook (23)Infosys (22)Agilent Technologies (19)Sage Software (17)Deshaw Inc (16)FlexTrade (15)
More Companies »
Software Engineer / Dev... (1062)Financial Software Deve... (170)Testing / Quality Assur... (56)Analyst (35)Virus Researcher (25)Field Sales (15)Developer Program Engin... (9)Front-end Software Engi... (6)MyJoB (5)area sales manager (4)Assistant (3)Cabin crew (2)Accountant (1)personnel (1)Intern (1)
Software Engineer in Te... (288)Program Manager (65)Development Support Eng... (47)INTERN(MSIDC) (28)Web Developer (18)System Administrator (10)Consultant (10)Production Engineer (5)Associate Technology L2 (5)AcquireKnowledge (4)Product Security Engine... (3)Solutions Architect (2)Gamer (1)mts (1)Fresh graduate interview (0)
More Job Titles »
Algorithm (1073)Terminology & Trivia (294)C (166)Object Oriented Design (159)Java (121)Testing (114)Arrays (101)Operating System (78)Database (70)Linked List (62)String Manipulation (56)Networking / Web / Inte... (44)Threads (36)Linux Kernel (33)PHP (22)
Coding (511)C++ (204)Behavioral (159)Data Structures (155)Experience (116)Brain Teasers (111)Computer Architecture &... (79)General Questions and C... (73)Trees and Graphs (69)Math & Computation (57)Application / UI Design (45)Ideas (38)System Design (35)Puzzles (30)Bit Manipulation (20)
More Topics »
CareerCup Official Interview Book Daily Questions Requests for Help Mock Interviews Video for Cracking the Coding Interview Job Placement Service CareerCup Blog
My Profile Edit Profile & Email Settings Sign Out