From 46bc080f117a968b3106a40b2fb38ac4d472547d Mon Sep 17 00:00:00 2001 From: Anand Soni Date: Thu, 12 Oct 2023 01:18:29 +0530 Subject: [PATCH] Euclid's Algo --- README.md | 2 +- gcd_strings.java | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 gcd_strings.java diff --git a/README.md b/README.md index a645923..7fcf9e5 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,4 @@ SR No | Program | Author 22 | Prime Number program | [Moola Pravalesh](https://github.com/MoolaPravalesh19) 23 | Caesar Cipher Program | [Ankush Dutta](https://github.com/GenDelta) 24 | Kadane's Algorithm | [Deepak Maurya](https://github.com/deepakmaur) - +25 | GCD of Strings | [Anand](https://github.com/prototype47) diff --git a/gcd_strings.java b/gcd_strings.java new file mode 100644 index 0000000..a2bc6b3 --- /dev/null +++ b/gcd_strings.java @@ -0,0 +1,25 @@ +import java.util.*; +class Solution +{ + public String gcdStrings(String st1, String st2) + { + if (!(st1 + st2).equals(st2 + st1)) return ""; + int gcd = gcd(st1.length(), st2.length()); + return st1.substring(0, gcd); + } + private int gcd(int a, int b) + { + return b == 0 ? a : gcd(b, a % b); + } +} +// class Main +// { +// public static void main(String[] args) +// { +// Scanner sc = new Scanner(System.in); +// String st1 = sc.next(); +// String st2 = sc.next(); +// Solution obj = new Solution(); +// System.out.println(obj.gcdStrings(st1, st2)); +// } +// }