我们有两个字符串,我们需要检查字符串是否是字谜。
null
例如:
Input : "anagram", "nagaram" Output : yes Input : "mat", "fat" Output : no
我们使用以下内置函数来解决此问题:
- 伯爵 :此函数通常具有语法 计数字符(字符串,返回模式) 用于执行与字符串相关的多个操作。此参数是可选的。它将值设置为0、1、2、3、4。
使用上述函数解决此问题的方法是,使用值1,即在所选模式下,上述函数将返回一个包含键值对的数组,其中键是ASCII值,相应的值是该ASCII值的出现次数。该数组只拥有频率大于0的ASCII值键。
// 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' ). "" ); print_r(is_anagram( 'card' , 'cart' ). "" ); ?> |
输出:
yes no
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END