Skip to main content
deleted 22 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Here is my code for checking to see if two strings are anagrams. This seems a little short, am. Am I missing anything? TimeBoth time and space complexity seemsseem to be O(1), space complexity O(1) as well?\$O(1)\$.

//Determine if a string is an anagram
//Time complexity: O(1) 
//Space complexity: O

#include <iostream>
#include <string>
using namespace std;

bool anagram(string one, string two) 
{

    if (one.length() != two.length())
    {   
        return false;
    }

    //sorting the strings
    sort(one.begin(), one.end());
    sort(two.begin(), two.end());

    return (one == two);

}

int main() {

    string one;
    string two;

    cout << "please enter in a string: ";
    cin >> one;
    cout << "please enter in another string: ";
    cin >> two;

    cout << anagram(one, two) <<endl;

    return 0;

}

Here is my code for checking to see if two strings are anagrams. This seems a little short, am I missing anything? Time complexity seems to be O(1), space complexity O(1) as well?

//Determine if a string is an anagram
//Time complexity: O(1) 
//Space complexity: O

#include <iostream>
#include <string>
using namespace std;

bool anagram(string one, string two) 
{

    if (one.length() != two.length())
    {   
        return false;
    }

    //sorting the strings
    sort(one.begin(), one.end());
    sort(two.begin(), two.end());

    return (one == two);

}

int main() {

    string one;
    string two;

    cout << "please enter in a string: ";
    cin >> one;
    cout << "please enter in another string: ";
    cin >> two;

    cout << anagram(one, two) <<endl;

    return 0;

}

Here is my code for checking to see if two strings are anagrams. This seems a little short. Am I missing anything? Both time and space complexity seem to be \$O(1)\$.

//Determine if a string is an anagram
//Time complexity: O(1) 
//Space complexity: O

#include <iostream>
#include <string>
using namespace std;

bool anagram(string one, string two) 
{

    if (one.length() != two.length())
    {   
        return false;
    }

    //sorting the strings
    sort(one.begin(), one.end());
    sort(two.begin(), two.end());

    return (one == two);

}

int main() {

    string one;
    string two;

    cout << "please enter in a string: ";
    cin >> one;
    cout << "please enter in another string: ";
    cin >> two;

    cout << anagram(one, two) <<endl;

    return 0;

}
Source Link

Checking if two strings are anagrams

Here is my code for checking to see if two strings are anagrams. This seems a little short, am I missing anything? Time complexity seems to be O(1), space complexity O(1) as well?

//Determine if a string is an anagram
//Time complexity: O(1) 
//Space complexity: O

#include <iostream>
#include <string>
using namespace std;

bool anagram(string one, string two) 
{

    if (one.length() != two.length())
    {   
        return false;
    }

    //sorting the strings
    sort(one.begin(), one.end());
    sort(two.begin(), two.end());

    return (one == two);

}

int main() {

    string one;
    string two;

    cout << "please enter in a string: ";
    cin >> one;
    cout << "please enter in another string: ";
    cin >> two;

    cout << anagram(one, two) <<endl;

    return 0;

}