题解 | #牛群密码 - 有效回文#
牛群密码 - 有效回文
https://www.nowcoder.com/practice/98fad63b47544d5ebf4042fc53b54b3d
class Solution{ public: bool isValidPalindrome(string password, int k) { unordered_map<char,int> Hashmap; unordered_map<char,int> :: iterator it; int len = password.size(); for(int i=0;i<len;i++){ it = Hashmap.find(password[i]); if(it!=Hashmap.end()){ it->second++; }else{ Hashmap[password[i]]=1; } } int size_map = Hashmap.size(); if(size_map<=k){ vector<int> count(size_map,1); int k=0; for(it=Hashmap.begin();it!=Hashmap.end();it++,k++){ //cout << "打印hash的it" << endl; //cout << it->first << endl; vector<char> vec; int len = password.length(); for(int i=0;i<len;i++){ if(password[i]!=it->first) vec.push_back(password[i]); else continue; } /* cout << "打印vec数组" << endl; for(const auto& it:vec){ cout << it; } cout << endl; */ int n = vec.size(); int i=0,j=n-1; while(i<=int(n/2)&&j>=int(n/2)&&i!=j){ //cout << vec[i] << " " << vec[j]<<endl; if(vec[i]!=vec[j])count[k]=0; i++; j--; } } int sum=0; for(const auto& num:count){ sum= sum|num; } if(sum==0)return false; }else return false; return true; } };