Danny has a possible list of passwords for Manny's Facebook account. All password length is odd. But Danny knows that Manny is a big fan of palindromes. So, his password and reverse of his password both should be on the list.
You have to print the length of Manny's password and its middle character.
Example:
Input: n = 4, s = {"abc", "def", "feg", "cba"}
Output: 3 b
Approach
C++
#include <bits/stdc++.h>using namespace std;void password(int n, vector<string> &s){set<string> st;for (int i = 0; i < n; i++){st.insert(s[i]);}string res;for (int i = 0; i < n; i++){string rev = "";for (int j = s[i].size() - 1; j >= 0; j--)rev += s[i][j];if (st.find(rev) != st.end()){res = s[i];break;}}cout << res.size() << " " << res[res.size() / 2] << "\n";}int main(){int n = 4;vector<string> s = {"abc", "def", "feg", "cba"};password(n, s);return 0;}
No comments:
Post a Comment