我们有两个字符串,我们需要检查字符串是否是Anagram。
例子
1 2 3 4 5 | Input : "anagram", "nagaram" Output : yes Input : "mat", "fat" Output : no |
我们使用以下内建函数来解决这个问题:
count_chars():该函数一般具有count_chars(string, return_mode)语法,用于执行与字符串相关的多个操作。这个参数是可选的。它取0 1 2 3 4。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // PHP code to check a given string is an anagram // of another or not <?php function is_anagram($string_1, $string_2) { if (count_chars($string_1, 1) == count_chars($string_2, 1)) return 'yes'; else return 'no'; } // Driver code print_r(is_anagram('program', 'grampro')."\n"); print_r(is_anagram('card', 'cart')."\n"); ?> |
输出
1 2 | yes no |