Question

In: Computer Science

Provide and implement three completely different algorithms of different running time that will check if two...

Provide and implement three completely different algorithms of different running time that will check if two strings are anagrams.

Solutions

Expert Solution

1.#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;

}

2.#include <bits/stdc++.h>

using namespace std;

#define NO_OF_CHARS 256

/* function to check whether two strings are anagram of

each other */

bool areAnagram(char* str1, char* str2)

{

// Create 2 count arrays and initialize all values as 0

int count1[NO_OF_CHARS] = { 0 };

int count2[NO_OF_CHARS] = { 0 };

int i;

// For each character in input strings, increment count

// in the corresponding count array

for (i = 0; str1[i] && str2[i]; i++) {

count1[str1[i]]++;

count2[str2[i]]++;

}

// If both strings are of different length. Removing

// this condition will make the program fail for strings

// like "aaca" and "aca"

if (str1[i] || str2[i])

return false;

// Compare count arrays

for (i = 0; i < NO_OF_CHARS; i++)

if (count1[i] != count2[i])

return false;

return true;

}

/* Driver code*/

int main()

{

char str1[] = "patna";

char str2[] = "ranchi";

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

}

3.#include <bits/stdc++.h>

using namespace std;

#define NO_OF_CHARS 256

bool areAnagram(char* str1, char* str2)

{

// Create a count array and initialize all values as 0

int count[NO_OF_CHARS] = { 0 };

int i;

// For each character in input strings, increment count

// in the corresponding count array

for (i = 0; str1[i] && str2[i]; i++) {

count[str1[i]]++;

count[str2[i]]--;

}

// If both strings are of different length. Removing

// this condition will make the program fail for strings

// like "aaca" and "aca"

if (str1[i] || str2[i])

return false;

// See if there is any non-zero value in count array

for (i = 0; i < NO_OF_CHARS; i++)

if (count[i])

return false;

return true;

}

// Driver code

int main()

{

char str1[] = "patna";

char str2[] = "atpan";

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

}

4.#include <bits/stdc++.h>

using namespace std;

bool isAnagram(string c, string d)

{

if (c.size() != d.size())

return false;

int count = 0;

// Take sum of all characters of first String

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

count += c[i];

}

// Subtract the Value of all the characters of second

// String

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

count -= d[i];

}

// If Count = 0 then they are anagram

// If count > 0 or count < 0 then they are not anagram

return (count == 0);

}

// Driver code

int main()

{

char str1[] = "shaba";

char str2[] = "shabaahmad";

// Function call

if (isAnagram(str1, str2))

cout << "The two strings are anagram of each other";

else

cout << "The two strings are not anagram of each "

"other";

return 0;

}


Related Solutions

In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection sort and Bubble Sort. The objective of this project is to understand the relation between the theoretical asymptotic complexities (Big-O) computed in class and the actual running times for different kinds of input. The enclosed source file main.cpp contains a program that reads two inputs: a letter specifying which sorting algorithm to use (I for InsertionSort , S for SelectionSort and B for BubbleSort),...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set...
DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set of integers using only one array that contains all the integers. Test your programs and compute the CPU time for different sets of integers generated by a random number generator. Must sure have the CPU time.
IN JAVA Implement SRT scheduling algorithms based on following actions: The simulation maintains the current time,...
IN JAVA Implement SRT scheduling algorithms based on following actions: The simulation maintains the current time, t, which is initialized to 0 and is incremented after each simulation step. Each simulation step then consists of the following actions: repeat until Rᵢ == 0 for all n processes /* repeat until all processes have terminated */ while no process is active, increment t /* if no process is ready to run, just advance t */ choose active processes pᵢ to run...
IN JAVA Implement SJF scheduling algorithms based on following actions: The simulation maintains the current time,...
IN JAVA Implement SJF scheduling algorithms based on following actions: The simulation maintains the current time, t, which is initialized to 0 and is incremented after each simulation step. Each simulation step then consists of the following actions: repeat until Rᵢ == 0 for all n processes /* repeat until all processes have terminated */ while no process is active, increment t /* if no process is ready to run, just advance t */ choose active processes pᵢ to run...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
Explain types of web crawlers and different Algorithms they use ex:-bfs,dfs and implement sequential and concurrent...
Explain types of web crawlers and different Algorithms they use ex:-bfs,dfs and implement sequential and concurrent crawler using scrapy or any other tool please explain your code with comments
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT