Skip to content

This program offers a C++ solution to compute the Greatest Common Divisor (GCD) of two strings, showcasing an application of the Euclidean algorithm for string manipulation.

Notifications You must be signed in to change notification settings

danieldotwav/Greatest-Common-Divisor-of-Strings

Repository files navigation

Greatest Common Divisor of Strings

Introduction

A C++ solution for finding the Greatest Common Divisor (GCD) of two strings, extending the mathematical concept of GCD from numbers to strings.

Implementation Details

Algorithms

  • gcdOfStrings(string str1, string str2)

    • Finds the largest string that divides both str1 and str2.
    • Parameters: Two strings str1 and str2.
    • Returns: The GCD string.
  • gcd(int a, int b)

    • Implements the Euclidean algorithm to find the GCD of two numbers.
    • Parameters: Two integers a and b.
    • Returns: The GCD of a and b.

Complexity Analysis

  • gcdOfStrings
    • Time Complexity: O(n), where n is the length of the shorter string.
    • Space Complexity: O(1).

Code Snippet

string gcdOfStrings(string str1, string str2) {
	if (str1 + str2 != str2 + str1) {
		return "";
	}

	int gcdLength = gcd(str1.length(), str2.length());
	return str1.substr(0, gcdLength);
}
  • gcd
    • Time Complexity: O(log(min(a, b))).
    • Space Complexity: O(1) for iterative implementations.

Code Snippet

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

About

This program offers a C++ solution to compute the Greatest Common Divisor (GCD) of two strings, showcasing an application of the Euclidean algorithm for string manipulation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages