Knowee
Questions
Features
Study Tools

You are to write a function called `isAnagram2` that takes two strings and returns `True` if they are anagrams. You can assume that the strings are made up of lowercase alphabets. `isAnagram2` should not use the `sorted` function.- **Hints/Guide**: - `ord` gives you the numeric representation of a character. `ord('a') = 97`, `ord('b') = 98`, `ord('c') = 99`, ..., `ord('y') = 121`, `ord('z') = 122`. - You can only use concepts we have treated in class so far up to lists. No dictionaries. - You cannot import any library. - You cannot use more than 10 variables in your function.

Question

You are to write a function called isAnagram2 that takes two strings and returns True if they are anagrams. You can assume that the strings are made up of lowercase alphabets. isAnagram2 should not use the sorted function.- Hints/Guide: - ord gives you the numeric representation of a character. ord('a') = 97, ord('b') = 98, ord('c') = 99, ..., ord('y') = 121, ord('z') = 122. - You can only use concepts we have treated in class so far up to lists. No dictionaries. - You cannot import any library. - You cannot use more than 10 variables in your function.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python function that checks if two strings are anagrams:

def isAnagram2(s1, s2):
    # Check if length of both strings is equal
    if len(s1) != len(s2):
        return False

    # Create two lists of size 26 (for each letter in the alphabet)
    count1 = [0]*26
    count2 = [0]*26

    # Iterate over each character in the strings
    for i in range(len(s1)):
        # ord('a') = 97, so subtract 97 to get a value between 0 and 25
        pos1 = ord(s1[i]) - 97
        pos2 = ord(s2[i]) - 97

        # Increment the count at the corresponding position
        count1[pos1] += 1
        count2[pos2] += 1

    # Check if the counts are equal
    for i in range(26):
        if count1[i] != count2[i]:
            return False

    return True

This function works by counting the number of occurrences of each letter in the two strings. If the counts are the same for all letters, then the strings are anagrams. The ord function is used to convert each letter to a number between 0 and 25, which is used as an index into the count arrays.

This problem has been solved

Similar Questions

Write a program to check two strings are anagram or not.

wo words are anagrams if you can rearrange the letters from one to spell the other. For example, `'hello'` and `'olleh'` are anagrams. You are to write a function called `isAnagram1` which takes two strings and returns `True` if they are anagrams. You can assume that the strings are made up of lowercase alphabets. `isAnagram1` should make use of the `sorted` function.

Given 2 strings, check if they are anagrams. An anagram is a rearrangement of the letters of one word to form another word. In other words, some permutations of string A must be the same as string B.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line containing 2 space-separated strings.Output FormatFor each test case, print True/False, separated by a new line.Constraints10 points1 <= T <= 1001 <= len(S) <= 103'a' <= S[i] <= 'z'40 points1 <= T <= 1001 <= len(S) <= 105'a' <= S[i] <= 'z'ExampleInput4iamlordvoldemort tommarvoloriddleb hstop posthi heyOutputTrueFalseTrueFalse

Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Check whether the given two numbers are Anagram or not. Two numbers are said to be as anagram if both numbers formed with same digits.Input Format:Accept two integers from the userOutput Format:Anagram or Not

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.