A Journey 2 Eternity

Posts Tagged ‘Permutation

We are going to write a method to decide if one is a permutation of the other (i.e GOD is a permutation of DOG)

bool IsPermutation(const string &s, const string &t)
{
	if (s.length() != t.length()) {
		return false;
	}

	int letters[256];
	for (int i = 0; i < 256; i++) {
		letters[i] = 0;
	}

	// count the number of each char in s
	for (unsigned int i = 0; i < s.length(); i++) {
		letters[s.at(i)]++;
	}

	for (unsigned int i = 0; i < t.length(); i++) {
		int c = t.at(i);
		if(--letters[c] < 0) {
			return false;
		}
	}

	return true;
}

Pages

Categories

April 2017
M T W T F S S
« Aug    
 12
3456789
10111213141516
17181920212223
24252627282930

Blog Stats

  • 26,880 hits