Problem p02418 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

int main() {
  std::string str1, str2, str3, str4;
  std::cin >> str1;
  std::cin >> str2;
  int size = str2.size();
  bool result = false;

  for (int a = 0; a < str1.size(); a++) {
    if ((size + a) <= str1.size()) {
      str3 = str1.substr(a, size);
    } else {
      str3 = str1.substr(a, str1.size() - a);
      str4 = str1.substr(0, (size + a) - str1.size());
      str3 += str4;
    }
    if (str2 == str3) {
      result = true;
      break;
    }
  }
  if (result) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  char SAO, ALO;
  int count[26] = {0};
  int next[26] = {0};
  while (true) {
    cin >> SAO;
    if (SAO == '\\n')
      break;
    if (SAO == 'A' || SAO == 'a') {
      count[0]++;
    }
    if (SAO == 'B' || SAO == 'b') {
      count[1]++;
    }
    if (SAO == 'C' || SAO == 'c') {
      count[2]++;
    }
    if (SAO == 'D' || SAO == 'd') {
      count[3]++;
    }
    if (SAO == 'E' || SAO == 'e') {
      count[4]++;
    }
    if (SAO == 'F' || SAO == 'f') {
      count[5]++;
    }
    if (SAO == 'G' || SAO == 'g') {
      count[6]++;
    }
    if (SAO == 'H' || SAO == 'h') {
      count[7]++;
    }
    if (SAO == 'I' || SAO == 'i') {
      count[8]++;
    }
    if (SAO == 'J' || SAO == 'j') {
      count[9]++;
    }
    if (SAO == 'K' || SAO == 'k') {
      count[10]++;
    }
    if (SAO == 'L' || SAO == 'l') {
      count[11]++;
    }
    if (SAO == 'M' || SAO == 'm') {
      count[12]++;
    }
    if (SAO == 'N' || SAO == 'n') {
      count[13]++;
    }
    if (SAO == 'O' || SAO == 'o') {
      count[14]++;
    }
    if (SAO == 'P' || SAO == 'p') {
      count[15]++;
    }
    if (SAO == 'Q' || SAO == 'q') {
      count[16]++;
    }
    if (SAO == 'R' || SAO == 'r') {
      count[17]++;
    }
    if (SAO == 'S' || SAO == 's') {
      count[18]++;
    }
    if (SAO == 'T' || SAO == 't') {
      count[19]++;
    }
    if (SAO == 'U' || SAO == 'u') {
      count[20]++;
    }
    if (SAO == 'V' || SAO == 'v') {
      count[21]++;
    }
    if (SAO == 'W' || SAO == 'w') {
      count[22]++;
    }
    if (SAO == 'X' || SAO == 'x') {
      count[23]++;
    }
    if (SAO == 'Y' || SAO == 'y') {
      count[24]++;
    }
    if (SAO == 'Z' || SAO == 'z') {
      count[25]++;
    }
  }
  while (true) {
    cin >> ALO;
    if (ALO == '\\n')
      break;
    if (ALO == 'A' || ALO == 'a') {
      next[0]++;
    }
    if (ALO == 'B' || ALO == 'b') {
      next[1]++;
    }
    if (ALO == 'C' || ALO == 'c') {
      next[2]++;
    }
    if (ALO == 'D' || ALO == 'd') {
      next[3]++;
    }
    if (ALO == 'E' || ALO == 'e') {
      next[4]++;
    }
    if (ALO == 'F' || ALO == 'f') {
      next[5]++;
    }
    if (ALO == 'G' || ALO == 'g') {
      next[6]++;
    }
    if (ALO == 'H' || ALO == 'h') {
      next[7]++;
    }
    if (ALO == 'I' || ALO == 'i') {
      next[8]++;
    }
    if (ALO == 'J' || ALO == 'j') {
      next[9]++;
    }
    if (ALO == 'K' || ALO == 'k') {
      next[10]++;
    }
    if (ALO == 'L' || ALO == 'l') {
      next[11]++;
    }
    if (ALO == 'M' || ALO == 'm') {
      next[12]++;
    }
    if (ALO == 'N' || ALO == 'n') {
      next[13]++;
    }
    if (ALO == 'O' || ALO == 'o') {
      next[14]++;
    }
    if (ALO == 'P' || ALO == 'p') {
      next[15]++;
    }
    if (ALO == 'Q' || ALO == 'q') {
      next[16]++;
    }
    if (ALO == 'R' || ALO == 'r') {
      next[17]++;
    }
    if (ALO == 'S' || ALO == 's') {
      next[18]++;
    }
    if (ALO == 'T' || ALO == 't') {
      next[19]++;
    }
    if (ALO == 'U' || ALO == 'u') {
      next[20]++;
    }
    if (ALO == 'V' || ALO == 'v') {
      next[21]++;
    }
    if (ALO == 'W' || ALO == 'w') {
      next[22]++;
    }
    if (ALO == 'X' || ALO == 'x') {
      next[23]++;
    }
    if (ALO == 'Y' || ALO == 'y') {
      next[24]++;
    }
    if (ALO == 'Z' || ALO == 'z') {
      next[25]++;
    }
  }
  int x = 0;
  for (int a = 0; a < 25; a++) {
    if (count[a] < next[a]) {
      x = 1;
    }
  }
  if (x = 1) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}