Dataset Viewer
Auto-converted to Parquet
trajectory
list
[ { "content": "Write Python code to solve the task:\nMike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome. \n\nA palindrome is a string that reads the same backward as forward, for example strings \"z\", \"aaa\", \"aba\", \"abccba\" are palindromes, but strings \"codeforces\", \"reality\", \"ab\" are not.\n\nInput\n\nThe first and single line contains string s (1 ≤ |s| ≤ 15).\n\nOutput\n\nPrint \"YES\" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or \"NO\" (without quotes) otherwise. \n\nExamples\n\nInput\n\nabccaa\n\n\nOutput\n\nYES\n\n\nInput\n\nabbcca\n\n\nOutput\n\nNO\n\n\nInput\n\nabcda\n\n\nOutput\n\nYES", "role": "user" }, { "content": "Here is the Python code to solve the task:\n\n```python\ndef is_palindrome(s):\n return s == s[::-1]\n\ndef count_non_matching_pairs(s):\n count = 0\n for i in range(len(s) // 2):\n if s[i] != s[-i - 1]:\n count += 1\n if len(s) % 2 == 1 and s[len(s) // 2] != s[-(len(s) // 2) - 1]:\n count += 1\n return count\n\ndef main():\n s = input()\n if count_non_matching_pairs(s) > 1:\n print(\"NO\")\n else:\n print(\"YES\")\n\nif __name__ == \"__main__\":\n main()\n```\n\nThis code defines three functions: `is_palindrome` checks if a string is a palindrome, `count_non_matching_pairs` counts the number of non-matching pairs of characters in a string (i.e., the number of pairs that are not equal), and `main` is the main function that reads the input, counts the number of non-matching pairs, and prints the result.\n\nThe `main` function first checks if the input string `s` is a palindrome using the `is_palindrome` function. If it is not, it prints \"NO\". Otherwise, it counts the number of non-matching pairs using the `count_non_matching_pairs` function. If there is more than one non-matching pair, it prints \"NO\". Otherwise, it prints \"YES\".\n\nThe `count_non_matching_pairs` function iterates over the first half of the string and checks if each pair of characters is equal. If a pair is not equal, it increments the count. If the length of the string is odd, it also checks if the middle character is equal to the character at the corresponding position from the end of the string, and increments the count if they are not equal. The function returns the count of non-matching pairs.\n\nThe `is_palindrome` function simply checks if the input string is equal to its reverse, using slicing to reverse the string.", "role": "assistant" } ]
README.md exists but content is empty.
Downloads last month
7