8

CPP Program to Check if Two Strings are Anagrams

 1 year ago
source link: https://www.geeksforgeeks.org/videos/cpp-program-to-check-if-two-strings-are-anagrams/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CPP Program to Check if Two Strings are Anagrams

CPP Program to Check if Two Strings are Anagrams
  • 40 Views
  • 18/07/2022

In this video, we will create a C++ Program on how to check if Two Strings are Anagrams or not.

What are anagrams?
Anagrams are words or phrases you spell by rearranging the letters of another word or phrase.

For examples:
String 1: aabc
String 2: abca
Output: True

String 1: act
String 2: cat
Output: True

String 1: below
String 2: elbow
Output: True

Here in this video, we cover three different methods to check it:
1. Sorting: Sort both strings. Compare the sorted strings
2. Hashing: In this method, two arrays are used. 
- Iterate through every character of both strings and increment the count of characters in the corresponding count arrays. 
- Compare count arrays. 
- If both the count arrays are the same, then it returns true.
3. Using one hash: In this method, we use only one count array instead of two. We can increment the value in the count array for characters in str1 and decrement for characters in str2. At last, if all count values are equal to 0, then the two strings are anagrams of each other. 

Apart from that, we will see the time and space complexity of all mentioned methods as well.

CPP program to check if 2 strings are anagram of each other or not:
https://www.geeksforgeeks.org/cpp-program-to-check-whether-two-strings-are-anagram-of-each-other/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK