Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 25 additions & 0 deletions gcd_strings.java
Original file line number Diff line number Diff line change
@@ -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));
// }
// }