Counting Prefix And Suffix Pairs In Strings
Iterate through all index pairs (i, j) where i < j, check if words[i] is both prefix and suffix of words[j] using PHP's substr() function, return total count of true pairs.
3042. Count Prefix and Suffix Pairs I Difficulty: Easy Topics: Array, String, Trie, Rolling Hash, String Matching, Hash Function You are given a 0-indexed string array words. Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2: isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix1 and a suffix2 of str2, and false otherwise. For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false. Return an integer denoting the number of index pairs (i, j) suc...