I’m starting to learn C++
by doing a lot of exercises, and I need your help so I can improve,
Here is how it goes, I will try to solve an exercise and post both the exercise and my solution to it,
And I want you to point me to where / what improvements I can do to make my code better, this would help me both uncover missing parts that I didn’t know about, and better ways of coding,
Here it is
Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.
and here is my solution
#include <iostream>
#include <cassert>
#include <string>
#include <memory>
using namespace std;
bool isperm(const string& a, const string& b) {
if (a.length() != b.length())
return false;
bool* used = new bool[a.length()]();
for (int i = 0; i != a.length(); i++) {
for (int j = 0; j != a.length(); j++) {
if (a.at(i) == b.at(j) && used[j] != true) {
used[j] = true;
break;
}
}
}
unsigned test = 0;
for (; test != a.length() && used[test] != false; ++test);
if (test == a.length())
return true;
return false;
}
int main() {
assert(isperm("long string","string long") == true);
assert(isperm("long", "olng") == true);
assert(isperm("ll","lo") == false);
//assert(isperm("lll", "ooo") == true);
return 0;
}
side note : I included x86 tag because most of the compiler nitty gritty optimization are done at that level, and I want to see the perspective of it from that side as well.
it can't goes without thanking you for taking the time to read and help me (^_^)
Hey reader you might want to check this code improvements [ exercise 2 ] space encoder for URL