└── Palidrom Index.cpp /Palidrom Index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string ltrim(const string &); 6 | string rtrim(const string &); 7 | 8 | 9 | 10 | /* 11 | * Complete the 'palindromeIndex' function below. 12 | * 13 | * The function is expected to return an INTEGER. 14 | * The function accepts STRING s as parameter. 15 | */ 16 | 17 | int palindromeIndex(string s) { 18 | int i=0,j =s.size()-1; 19 | int ii=i, jj=j; 20 | bool error = false; 21 | if(s.size()>=3){ 22 | while(i<=j){ 23 | if(s[i]!=s[j]){ 24 | if(error) return jj; 25 | error = true; 26 | ii = i; 27 | jj = j; 28 | i++; 29 | } 30 | else{ 31 | i++, j--; 32 | } 33 | } 34 | } 35 | return error ? ii : -1; 36 | } 37 | 38 | int main() 39 | { 40 | ofstream fout(getenv("OUTPUT_PATH")); 41 | 42 | string q_temp; 43 | getline(cin, q_temp); 44 | 45 | int q = stoi(ltrim(rtrim(q_temp))); 46 | 47 | for (int q_itr = 0; q_itr < q; q_itr++) { 48 | string s; 49 | getline(cin, s); 50 | 51 | int result = palindromeIndex(s); 52 | 53 | fout << result << "\n"; 54 | } 55 | 56 | fout.close(); 57 | 58 | return 0; 59 | } 60 | 61 | string ltrim(const string &str) { 62 | string s(str); 63 | 64 | s.erase( 65 | s.begin(), 66 | find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) 67 | ); 68 | 69 | return s; 70 | } 71 | 72 | string rtrim(const string &str) { 73 | string s(str); 74 | 75 | s.erase( 76 | find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), 77 | s.end() 78 | ); 79 | 80 | return s; 81 | } 82 | 83 | --------------------------------------------------------------------------------