Amazon Interview Question
Backend DevelopersCountry: United States
1. First check if length of both the string is same or not.
2. traverse through target string and store the count of each char in a map/array.
3. now traverse through another string and decrement the count of the char from original store.
4. At the end if the store has a count of 0 for all the char return value true else false.
Another way is stating this problem is to say whether two strings are permutations of each other. I present two solutions. One using sorting which takes O(nlogn) where n = the number of characters/letters in the given word. Other approach is to use a hash table and maintain counts of letters. If the counts match, then the target string can be formed by swapping the characters in the original string.
Elegant solutions in python:
- prudent_programmer March 01, 2018