1027A - Palindromic Twist - CodeForces Solution


implementation strings *1000

Please click on ads to support us..

Python Code:


import bisect
from sys import stdin

input = stdin.readline

mod = 10 ** 9 + 7
eps = 10 ** -9


def linp(type=int):
    return list(map(type, input().split()))


def minp(type=int):
    return map(type, input().split())


def tinp(type=int):
    return type(input())


def __gcd(a, b):
    return a if b == 0 else __gcd(b, a % b)


def __lcm(a, b):
    return a * b / __gcd(a, b)


def __fact(n):
    return 1 if n == 1 else n * __fact(n - 1)


def __mex(a):
    mex = 0
    a.sort()
    for x in a:
        if x <= mex:
            mex += 1
        else:
            break
    return mex


def __dist(x1, y1, x2, y2):
    return (x1 - x2) ** 2 + (y1 - y2) ** 2


def __getprimes(n):
    isprime = [True for i in range(n + 1)]
    primes = []

    for i in range(2, n + 1):
        if isprime[i]:
            for j in range(i * i, n + 1, i):
                isprime[j] = False
    for i in range(2, n + 1):
        if isprime[i]:
            primes.append(i)
    return primes


def __getdividers(n):
    i = 1
    ret = []
    while i * i <= n:
        if n % i == 0:
            ret.append(i)

            if i * i != n:
                ret.append(n // i)
        i += 1
    ret.sort()
    return ret


def __modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if (m == 1):
		return 0
	
	while (a > 1):
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if (x < 0):
		x = x + m0
	return x

def __isprime(n):
    if n < 2:
        return False
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True


def __cntprimediv(n):
    ret = 0
    x = n
    i = 2
    while i * i <= x:
        while n % i == 0:
            n //= i
            ret += 1
        i += 1
    if n > 1:
        ret += 1
    return ret


def __primefactors(n):
    ret = []
    x = n
    i = 2
    while i * i <= x:
        while n % i == 0:
            ret.append(i)
            n //= i
        i += 1
    if n > 1:
        ret.append(n)
    return ret


def __sumdigit(n):
    ret = 0
    while n > 0:
        ret += n % 10
        n //= 10
    return ret


def __zfunc(s):
    n = len(s)
    z = [0 for i in range(n)]
    l = 0
    r = 0
    for i in range(1, n):
        if r >= i:
            z[i] = min(z[i - l], r - i + 1)
        while z[i] + i < n and s[z[i]] == s[z[i] + i]:
            z[i] += 1
        if i + z[i] - 1 > r:
            l = i
            r = i + z[i] - 1
    return z


def __to(n, x):
    ret = ''
    while n > 0:
        q = n % x
        if q < 10:
            ret += str(q)
        else:
            ret += chr(q - 10 + ord('a'))
        n //= x
    return ret[::-1]


def solve(t):
	n = tinp()
	s = input().strip()
	ok = True
	for i in range(n // 2):
		if s[i] != s[n - i - 1] and abs(ord(s[i]) - ord(s[n - i - 1])) != 2:
			ok = False
	print('YES' if ok else 'NO')

t = 1
t = int(input())

for i in range(t):
    solve(i + 1)

C++ Code:

#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    string s;
    cin>>s;
     
    bool flag=true;
    int j=n-1;
    for(int i=0;i<n/2;i++,j--){
        if(s[i]=='a'){
            if(s[j]!='a' && s[j]!='b' && s[j]!='c'){
                {
                    flag=false;
                    break;
                }
            }

            if(s[i]=='z'){
            if(s[j]!='z' && s[j]!='y' && s[j]!='x'){
                {
                    flag=false;
                    break;
                }
            }
        }

    }

    int ans=abs(s[i]-s[j]);
    if(ans>2 || ans==1 ){
        flag=false;
        break;
    }
   
   }
   if(flag)
   cout<<"YES"<<endl;
   else
   cout<<"NO"<<endl;
}
return 0;
}



Comments

Submit
0 Comments
More Questions

761A - Dasha and Stairs
1728B - Best Permutation
1728A - Colored Balls Revisited
276B - Little Girl and Game
1181A - Chunga-Changa
1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train