Computer Science, asked by arifulcmp28, 3 months ago

How do you print duplicate characters from a string?

How do you check if two strings are anagrams of each other?​

Answers

Answered by Itzkrushika156
3

Explanation:

How do you print duplicate characters from a string?

string = "Great responsibility";

print("Duplicate characters in a given string: ");

#Counts each character present in the string.

for i in range(0, len(string)):

count = 1;

for j in range(i+1, len(string)):

if(string[i] == string[j] and string[i] != ' '):

count = count + 1;

How do you check if two strings are anagrams of each other?

Method 1 (Use Sorting)

Sort both strings

Compare the sorted strings

Below is the implementation of the above idea:

// 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 = "test";

string str2 = "ttew";

// 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;

FOLLOW ME MARK AS BRAINLIST

Attachments:
Answered by Goldenjungkookie
4

Answer:

How do you print duplicate characters from a string?

A)string = "Great responsibility";

print("Duplicate characters in a given string: ");

#Counts each character present in the string

for i in range(0, len(string)):

count = 1;

for j in range(i+1, len(string)):

if(string[i] == string[j] and string[i] != ' '):

count = count + 1;

#Set string[j] to 0 to avoid printing visited character

string = string[:j] + '0' + string[j+1:];

#A character is considered as duplicate if count is greater than 1

if(count > 1 and string[i] != '0'):

print(string[i]);

How do you check if two strings are anagrams of each other?

A)Method 1 (Use Sorting)

Sort both strings

Compare the sorted strings

Below is the implementation of the above idea:

// 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 = "test";

string str2 = "ttew";

// 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;

}

Similar questions