Skip to main content

Command Palette

Search for a command to run...

Occurrences After Bigram

Published
2 min read
Occurrences After Bigram
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.

Given two strings first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second.

Return an array of all the words third for each occurrence of "first second third".

LeetCode Problem - 1078

class Solution {
    String first, second, text;
    String[] splitText;

    public String[] findOcurrences(String text, String first, String second) {
        // Initialize a list to store the result
        List<String> list = new ArrayList<>();

        // Store the input parameters for later use
        this.first = first;
        this.second = second;
        this.text = text;

        // Split the input text into words based on spaces
        String[] splitText = text.split(" ");
        this.splitText = splitText; // Save the split text for later comparison

        // Iterate through the words in the split text
        for (int i = 0; i < splitText.length; i++) {
            String str = splitText[i];

            // Check if the current word matches the first word
            if (str.equals(first) && (i != splitText.length - 1)) {
                // Check if the next word matches the second word, and ensure there are at least two more words after
                boolean flag = findThirdString(splitText[i + 1], i + 1);

                // If the second word matches, add the word after it (the third word) to the result list
                if (flag) {
                    list.add(splitText[i + 2]);
                }
            }
        }

        // Convert the list of results to an array and return it
        return list.toArray(new String[0]);
    }

    // Helper function to check if the second word is found after the first
    public boolean findThirdString(String str, int idx) {
        // Check if the word at the current index matches the second word
        String textIdx2 = splitText[idx];

        // Return true if the second word is found and there is at least one word after it
        if (textIdx2.equals(second) && idx != splitText.length - 1) {
            return true;
        }

        // Return false if the second word is not found
        return false;
    }
}

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.