Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anagrams in CPP #2018

Open
harshalijain14 opened this issue Oct 3, 2023 · 9 comments
Open

Anagrams in CPP #2018

harshalijain14 opened this issue Oct 3, 2023 · 9 comments

Comments

@harshalijain14
Copy link

I would like to add a program in CPP section to check if two strings are anagrams or not

@Shrehan1234
Copy link

#include
#include
#include

// Function to check if two strings are anagrams
bool areAnagrams(const std::string& str1, const std::string& str2) {
// Sort both strings
std::string sorted_str1 = str1;
std::string sorted_str2 = str2;
std::sort(sorted_str1.begin(), sorted_str1.end());
std::sort(sorted_str2.begin(), sorted_str2.end());

// Compare the sorted strings
return sorted_str1 == sorted_str2;

}

int main() {
std::string input1, input2;

// Input the first string
std::cout << "Enter the first string: ";
std::cin >> input1;

// Input the second string
std::cout << "Enter the second string: ";
std::cin >> input2;

// Check if the strings are anagrams and print the result
if (areAnagrams(input1, input2)) {
    std::cout << "The two strings are anagrams." << std::endl;
} else {
    std::cout << "The two strings are not anagrams." << std::endl;
}

return 0;

}

@Ammu003
Copy link

Ammu003 commented Oct 3, 2023

hello , can you please assign me this issue i want to contribute to it @DHEERAJHARODE .

Here is the sample code :

Approach 1: using sort (inbuilt functions to find anagrams )

code :

bool isAnagram(string a, string b)
{

if(a.length()!=b.length())
{
    return false;
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
return a==b;
    
}

description: In this we sort both strings and then compare if all elements are present or not . simple right ?

Approach 2: using unordered_map

code :

bool isAnagram(string a, string b){

   int m = a.size();
    int n = b.size();
    if (m != n) {
        return false;                               // If the lengths are different they can't be anagrams
    }
    unordered_map<char, int> s1;
    unordered_map<char, int> s2;
    for (int i = 0; i < m; i++) {
        s1[a[i]]++;
        s2[b[i]]++;
    }
    for (int i = 0; i < m; i++) {
        if (s1[a[i]] != s2[a[i]]) {
            return false;
        }
    }
    return true;                      // Should be placed outside the loop
}

@ayush645
Copy link

ayush645 commented Oct 3, 2023

public boolean isAnagram(String s, String t) {
int [] count = new int[26];

    if(s.length() != t.length()){
        return false;
    }

    for(int i =0;i<s.length();i++){
        count[s.charAt(i) -'a']++;
        count[t.charAt(i) -'a']--;

    }

    for(int i = 0;i<26;i++){
        if(count[i] !=0){
            return false;
        }
    }

    return true;
}

can you please assign to me @DHEERAJHARODE

@Abhi7026
Copy link

Abhi7026 commented Oct 3, 2023

can you please assign to me @DHEERAJHARODE

@SONUKUMARRAM145
Copy link

SONUKUMARRAM145 commented Oct 4, 2023

hello can you pls assign this issue to me , I want to contribute in this issue thank you

@saku1331
Copy link

saku1331 commented Oct 4, 2023

I wish to contribute. Kindly assign this to me.

@mohit8740
Copy link

// C++ program to check whether two strings are anagrams
// of each other
#include <bits/stdc++.h>
using namespace std;

/* function to check whether two strings are anagram of
each other */
bool areAnagram(string str1, string str2)
{
// Get lengths of both strings
int n1 = str1.length();
int n2 = str2.length();

// If length of both strings is not same, then they
// cannot be anagram
if (n1 != n2)
	return false;

// Sort both the strings
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());

// Compare sorted strings
for (int i = 0; i < n1; i++)
	if (str1[i] != str2[i])
		return false;

return true;

}

// Driver code
int main()
{
string str1 = "gram";
string str2 = "arm";

// Function Call
if (areAnagram(str1, str2))
	cout << "The two strings are anagram of each other";
else
	cout << "The two strings are not anagram of each "
			"other";

return 0;

}

@kshitijdshah99
Copy link

Hey, @DHEERAJHARODE I am intrested to contribute to this issue pls assign this issue to me...I have solution for this

@Ashima2003
Copy link

I wish to contribute. Kindly assign me this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants