Skip to main content

Command Palette

Search for a command to run...

Count Prefix and Suffix Pairs I

Published
1 min read
Count Prefix and Suffix Pairs I
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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 prefix and a suffix 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) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.

LeetCode Problem - 3042

class Solution {
    public int countPrefixSuffixPairs(String[] words) {
        // Initialize a counter to keep track of the number of prefix-suffix pairs
        int count = 0;

        // Iterate through each word in the array
        for (int i = 0; i < words.length; i++) {
            // Store the current word in a temporary variable
            String tempI = words[i];

            // Iterate through the remaining words in the array
            for (int j = i + 1; j < words.length; j++) {
                // Store the next word in a temporary variable
                String tempJ = words[j];

                // Check if the next word starts with and ends with the current word
                if (tempJ.startsWith(tempI) && tempJ.endsWith(tempI)) {
                    // If it does, increment the count of prefix-suffix pairs
                    count++;
                }
            }
        }

        // Return the total count of prefix-suffix pairs
        return count;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.