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)returnstrueifstr1is both a prefix1 and a suffix2 ofstr2, andfalseotherwise.
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) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.
Example 1:
- Input: words = ["a","aba","ababa","aa"]
- Output: 4
- Explanation: In this example, the counted index pairs are: i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true. i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true. i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true. i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true. Therefore, the answer is 4.
Example 2:
- Input: words = ["pa","papa","ma","mama"]
- Output: 2
- Explanation: In this example, the counted index pairs are: i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true. i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true. Therefore, the answer is 2.
Example 3:
- Input: words = ["abab","ab"]
- Output: 0
- Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false. Therefore, the answer is 0.
Constraints:
1 <= words.length <= 501 <= words[i].length <= 10-
words[i]consists only of lowercase English letters.
Hint:
- Iterate through all index pairs
(i, j), such thati < j, and checkisPrefixAndSuffix(words[i], words[j]). - The answer is the total number of pairs where
isPrefixAndSuffix(words[i], words[j]) == true.
Solution:
We need to iterate through all index pairs (i, j) where i < j and check whether the string words[i] is both a prefix and a suffix of words[j]. For each pair, we can use PHP's built-in functions substr() to check for prefixes and suffixes.
Let's implement this solution in PHP: 3042. Count Prefix and Suffix Pairs I
<?php
/**
* @param String[] $words
* @return Integer
*/
function countPrefixAndSuffixPairs($words) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* Function to check if str1 is both a prefix and a suffix of str2
*
* @param $str1
* @param $str2
* @return bool
*/
function isPrefixAndSuffix($str1, $str2) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example Test Cases
$words1 = ["a", "aba", "ababa", "aa"];
$words2 = ["pa", "papa", "ma", "mama"];
$words3 = ["abab", "ab"];
echo countPrefixAndSuffixPairs($words1) . "\n"; // Output: 4
echo countPrefixAndSuffixPairs($words2) . "\n"; // Output: 2
echo countPrefixAndSuffixPairs($words3) . "\n"; // Output: 0
?>
Explanation:
-
countPrefixAndSuffixPairs($words):
- This function loops through all possible index pairs
(i, j)such thati < j. - It calls
isPrefixAndSuffix()to check ifwords[i]is both a prefix and a suffix ofwords[j]. - If the condition is true, it increments the count.
- This function loops through all possible index pairs
-
isPrefixAndSuffix($str1, $str2):
- This helper function checks whether
str1is both a prefix and a suffix ofstr2. - It uses
substr()to extract the prefix and suffix ofstr2and compares them withstr1. - If both conditions are true, it returns
true, otherwise, it returnsfalse.
- This helper function checks whether
Time Complexity:
- The time complexity is O(n2 x m), where
nis the length of thewordsarray, andmis the average length of the strings in the array. This is due to the nested loops and thesubstr()operations.
Example Output:
For the given input arrays:
-
["a", "aba", "ababa", "aa"]-> Output:4 -
["pa", "papa", "ma", "mama"]-> Output:2 -
["abab", "ab"]-> Output:0
This solution should work efficiently within the given constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)