0% found this document useful (0 votes)
0 views2 pages

Q2b.cpp

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Q2b.cpp

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <random>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>

using namespace std;

string random_word(int L) {
std::string s;
for (int i = 0; i < L; ++i) {
s.push_back('a' + std::random_device()() % 26);
}
return s;
}

bool isanagram( string& a, string& b) {


if (a.length() != b.length()) {
return false;
}

string sorted_a = a;
string sorted_b = b;

std::sort(sorted_a.begin(), sorted_a.end());
std::sort(sorted_b.begin(), sorted_b.end());

return sorted_a == sorted_b;


}

void count_anagrams(const vector<string>& v) {


int count = 0;

for (int i = 0; i < v.size(); ++i) {


for (int j = i + 1; j < v.size(); ++j) {
if (isanagram(v[i], v[j])) {
++count;
}
}
}

cout << "Number of anagram pairs: " << count << endl;
}

int main() {
srand(time(0));

vector<string> v;
for (int i = 0; i < 90000; ++i) {
int L = (rand() % 20) + 1;
v.push_back(random_word(L));
}

for (int i = 0; i < 10000; ++i) {


string s = v[i];
shuffle(s.begin(), s.end(), std::default_random_engine(std::random_device{}
()));
v.push_back(s);
}

count_anagrams(v);

You might also like