library(magrittr)
tt <- outer(letters, letters, paste0) %>% as.vector
input <- c(" aaba aa", " abbacazz ")
print(input)
# [1] " aaba aa"   " abbacazz "

res <-
  matrix(NA_integer_, length(input), length(tt)) %>%
  set_colnames(tt) %>%
  set_rownames(input)

for (i in 1:length(tt)) {
  res[, i] <-
    gregexpr(tt[i], input) %>%
    sapply(., function(k) {
      if (k[1] == -1) {
        0L
      } else {
        length(k)
      }
    })
}

res[, 1:20]
res[, c("ab", "ca", "az", "zz")]
