Created
March 16, 2018 07:17
-
-
Save yao2030/ec5c4978084feda26695ace67bdce4b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function is_onestep_palidrome(str) { | |
function is_palindrome(str_) { | |
return str_ == str_.split('').reverse().join(''); | |
} | |
function swapStr(str, first, last) { | |
if (first == last) { | |
return str; | |
} | |
if (last < first) { | |
var temp = last; | |
last = first; | |
first = temp; | |
} | |
if (first >= str.length) { | |
return str; | |
} | |
return str.substring(0, first) + | |
str[last] + | |
str.substring(first + 1, last) + | |
str[first] + | |
str.substring(last + 1); | |
} | |
var diff = []; | |
var start = 0; | |
var end = str.length - 1; | |
while (start < end) { | |
if (str.charAt(start) != str.charAt(end)) { | |
diff.push(start); | |
} | |
start++; | |
end--; | |
} | |
if (diff.length === 0) { | |
print('YES'); | |
return; | |
} | |
if (diff.length == 1) { | |
if (is_palindrome(swapStr(str, diff[0], Math.floor(str.length / 2))) || | |
is_palindrome(swapStr(str, str.length - 1 - diff[0], Math.floor(str.length / 2))) | |
) { | |
print('YES'); | |
} else { | |
print('NO'); | |
} | |
return; | |
} | |
if (diff.length == 2) { | |
if (is_palindrome(swapStr(str, diff[0], diff[1])) || | |
is_palindrome(swapStr(str, diff[0], str.length - 1 - diff[1])) || | |
is_palindrome(swapStr(str, diff[1], str.length - 1 - diff[0])) | |
) { | |
print('YES'); | |
} else { | |
print('NO'); | |
} | |
return; | |
} | |
print('NO'); | |
} | |
is_onestep_palidrome(readline().trim()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment