Valid Anagram

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 70 :- Valid Anagram

A valid anagram problem involves determining if two given strings are anagrams of each other. 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. For example, "listen" and "silent" are anagrams of each other.

To solve the valid anagram problem, we can count the occurrences of each character in both strings and check if the character counts are the same for both strings. If they are the same, the strings are anagrams; otherwise, they are not.

Here's the C++ implementation to check if two strings are valid anagrams:

cppCopy code

#include <iostream> #include <unordered_map> #include <string> bool areAnagrams(const std::string& s1, const std::string& s2) {    if (s1.length() != s2.length()) {        return false;    }    // Count the occurrences of characters in s1    std::unordered_map<char, int> charCount;    for (char c : s1) {        charCount[c]++;    }    // Decrement the character count for s2    for (char c : s2) {        charCount[c]--;    }    // If any character count is non-zero, it's not an anagram    for (const auto& entry : charCount) {        if (entry.second != 0) {            return false;        }    }    return true; } int main() {    std::string s1, s2;    std::cout << "Enter the first string: ";    std::cin >> s1;    std::cout << "Enter the second string: ";    std::cin >> s2;    if (areAnagrams(s1, s2)) {        std::cout << "The strings are valid anagrams.\n";    } else {        std::cout << "The strings are not valid anagrams.\n";    }    return 0; }

Example Input:

cCopy code

Enter the first string: listen Enter the second string: silent

Example Output:

sqlCopy code

The strings are valid anagrams.

In this code, we use an unordered_map to count the occurrences of each character in the first string (s1). Then, we decrement the character count for each character in the second string (s2). Finally, we check if all character counts are zero, indicating that both strings are anagrams. The time complexity of this algorithm is O(N), where N is the length of the strings.

11. Week5 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?