#include #include using namespace std; string sort(string &s) { for (int i = s.length() - 1; i >= 1; i--) { // Find the maximum in the list[0..i] char currentMax = s[0]; int currentMaxIndex = 0; for (int j = 1; j <= i; j++) { if (currentMax < s[j]) { currentMax = s[j]; currentMaxIndex = j; } } // Swap list[i] with list[currentMaxIndex] if necessary; if (currentMaxIndex != i) { s[currentMaxIndex] = s[i]; s[i] = currentMax; } } return s; } bool isAnagram(string const &s1, string const &s2) { string newS1 = string(s1); string newS2 = string(s2); sort(newS1); sort(newS2); if (newS1.length() != newS2.length()) return false; for (int i = 0; i < newS1.length(); i++) { if (newS1[i] != newS2[i]) return false; } return true; } int main() { cout << (isAnagram("silent", "listen") ? "true" : "false") << endl; cout << (isAnagram("garden", "ranged") ? "true" : "false") << endl; cout << (isAnagram("split", "lisp") ? "true" : "false") << endl; return 0; }