Here's the C ++ implementation:
sourceCpp(' bool isPalindrome(String x) { std::string y(x); int n = y.size(); for(int i = 0; i < n / 2; ++i) { if (y[i] != y[n - i - 1]) return false; } return true; } ')
It also does not work with non-ASCII strings, but it is about 10 times faster than a pure R solution:
library(microbenchmark) options(digits = 3) is.palindrome <- function (word) { identical(word, paste(rev(strsplit(word, "")[[1]]), collapse="")) } x <- paste(letters, rev(letters), collapse = "") y <- paste("a", x) microbenchmark( is.palindrome(x), isPalindrome(x), is.palindrome(y), isPalindrome(y) )
hadley
source share