Untitled

 avatar
unknown
c_cpp
2 years ago
1.8 kB
3
Indexable
#include <iostream>
#include <string>

using namespace std;

string checkSimilarPasswords(string newPasswords[], string oldPasswords[], int n) {
  // Create a map to store the next cyclic character for each character.
  map<char, char> nextChar;
  for (char c = 'a'; c <= 'z'; c++) {
    nextChar[c] = (c + 1) % 26;
  }

  // Iterate over the pairs of passwords.
  string results[n];
  for (int i = 0; i < n; i++) {
    // Initialize a boolean array to track if the character at each index is present in the old password.
    bool present[newPasswords[i].length()] = {false};
    for (int j = 0; j < oldPasswords[i].length(); j++) {
      if (oldPasswords[i][j] == newPasswords[i][j]) {
        present[j] = true;
      }
    }

    // Iterate over the indices of the new password and try to find a matching character in the old password.
    bool found = false;
    for (int j = 0; j < newPasswords[i].length(); j++) {
      char c = newPasswords[i][j];
      char next = nextChar[c];
      if (!present[j] && present[j - 1]) {
        if (oldPasswords[i].find(next) != string::npos) {
          present[j] = true;
          found = true;
          break;
        }
      }
    }

    // If a matching character was not found, then the passwords are not similar.
    if (!found) {
      results[i] = "NO";
    } else {
      results[i] = "YES";
    }
  }

  return results;
}

int main() {
  string newPasswords[] = {"baacbab", "accdb", "baacba"};
  string oldPasswords[] = {"abdbc", "ach", "abb"};
  int n = sizeof(newPasswords) / sizeof(newPasswords[0]);

  string results = checkSimilarPasswords(newPasswords, oldPasswords, n);
  for (int i = 0; i < n; i++) {
    cout << results[i] << endl;
  }

  return 0;
}
Use code with caution. Learn more
This code first creates a map to stor
Editor is loading...