Skip to main content

Command Palette

Search for a command to run...

Greatest Common Divisor of Strings

Published
1 min read
Greatest Common Divisor of Strings
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.

For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times).

Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.

LeetCode Problem - 1071

class Solution {
    public String gcdOfStrings(String str1, String str2) {
        // If the concatenation of the two strings is equal when their order is swapped,
        // then the common divisor of their lengths is the greatest common divisor of their lengths.
        if ((str1+str2).equals(str2+str1)){
            // Return a substring of str1 with length equal to the greatest common divisor.
            return str1.substring(0, gcd(str1.length(), str2.length()));
        }
        // If no common divisor is found, return an empty string.
        return "";
    }

    // This method calculates the greatest common divisor using Euclid's algorithm.
    private int gcd(int a, int b){
        // Base case: if b is 0, then a is the greatest common divisor.
        return b==0 ? a : gcd(b, a%b);
    }
}

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.