함수 isRotatedString()

1 PHP[ | ]

function is_rotated_string($r, $o) {
    if( strlen($r) != strlen($o) ) return false;
    return (strpos($o.$o, $r) !== false );
}

var_dump(is_rotated_string("abcd", "abcd")); # bool(true)
var_dump(is_rotated_string("bcda", "abcd")); # bool(true)
var_dump(is_rotated_string("cdab", "abcd")); # bool(true)
var_dump(is_rotated_string("dabc", "abcd")); # bool(true)

var_dump(is_rotated_string("abc", "abcd"));  # bool(false)
var_dump(is_rotated_string("dcba", "abcd")); # bool(false)
var_dump(is_rotated_string("adcb", "abcd")); # bool(false)
var_dump(is_rotated_string("badc", "abcd")); # bool(false)

2 R[ | ]

isRotatedString <- function(r, o) {
    if( nchar(r) != nchar(o) ) return(FALSE)
    grepl(r, paste(o,o,sep=""))
}

print(isRotatedString("abcd", "abcd")) ## [1] TRUE
print(isRotatedString("bcda", "abcd")) ## [1] TRUE
print(isRotatedString("cdab", "abcd")) ## [1] TRUE
print(isRotatedString("dabc", "abcd")) ## [1] TRUE

print(isRotatedString("abc", "abcd"))  ## [1] FALSE
print(isRotatedString("dcba", "abcd")) ## [1] FALSE
print(isRotatedString("adcb", "abcd")) ## [1] FALSE
print(isRotatedString("badc", "abcd")) ## [1] FALSE

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}