QUESTION ANSWERSViews: 8045Share  Posted by - Anonymous

hackerrank interview question fun with anagrams

Problem statement:

Given an array of strings, remove each string that is an anagram of an earlier string, then return the remaining array in sorted order.


Example

str = ['code', 'doce', 'ecod', 'framer', 'frame']

  • code and doce are anagrams. Remove doce from the array and keep the first occurrence code in the array.
  • code and ecod are anagrams. Remove ecod from the array and keep the first occurrence code in the array.
  • code and framer are not anagrams. Keep both strings in the array.
  • framer and frame are not anagrams due to the extra r in framer. Keep both strings in the array.
  • Order the remaining strings in ascending order: ['code','frame','framer'].



here you can find solution for to find anagrams


#!/bin/python3


import math

import os

import random

import re

import sys


#

# Complete the 'funWithAnagrams' function below.

#

# The function is expected to return a STRING_ARRAY.

# The function accepts STRING_ARRAY text as parameter.

#


def checkForAnagrams(word, arr):

    for x in arr:

        if (sorted(word) == sorted(x)):

            return True

    return False


def funWithAnagrams(text):

    limit = len(text)

    text.reverse()

    final_text = list(text)


    count = 0

    for i in range(0, limit):

        if text[i+1:] and checkForAnagrams(text[i], text[i+1:]):

            final_text.pop(i - count)

            count += 1


    return sorted(final_text)


if __name__ == '__main__':

    fptr = open(os.environ['OUTPUT_PATH'], 'w')


    text_count = int(input().strip())


    text = []


    for _ in range(text_count):

        text_item = input()

        text.append(text_item)


    result = funWithAnagrams(text)


    fptr.write('\n'.join(result))

    fptr.write('\n')


    fptr.close()


The Best Place To Learn Anything Coding Related - https://bit.ly/3MFZLIZ Join my free exclusive community built to empower ...

Fun with Anagrams

Making Anagrams. Solution Approach with code example. HackerRank Challenge

94 - Anagram | Strings | Hackerrank Solution | Python

Amazon D-E-shaw Snapdeal Interview Question | Anagram Palindrome #100DaysOfCode | code io | English


Question: Mary is an anagram of Army


function isAnagram(str1, str2){

let str1Arr = str1.split("");

let str2Arr = str2.split("");


str1Arr.sort();

str2Arr.sort();

if(str1Arr.join("") === str2Arr.join("")){

return true;

}

return false;

}

Posted by WebxTutor Training

Views -