Skip to content

Commit 0a61045

Browse files
committed
leetcode
1 parent 4ca5a8b commit 0a61045

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
3+
4+
5+
-* 1071. Greatest Common Divisor of Strings *-
6+
7+
8+
For two strings s and t, we say "t divides s" if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times).
9+
10+
Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.
11+
12+
13+
14+
Example 1:
15+
16+
Input: str1 = "ABCABC", str2 = "ABC"
17+
Output: "ABC"
18+
Example 2:
19+
20+
Input: str1 = "ABABAB", str2 = "ABAB"
21+
Output: "AB"
22+
Example 3:
23+
24+
Input: str1 = "LEET", str2 = "CODE"
25+
Output: ""
26+
27+
28+
Constraints:
29+
30+
1 <= str1.length, str2.length <= 1000
31+
str1 and str2 consist of English uppercase letters.
32+
33+
*/
34+
class A {
35+
String gcdOfStrings(String str1, String str2) {
36+
return (str1 + str2 == str2 + str1)
37+
? str1.substring(0, str1.length.gcd(str2.length))
38+
: "";
39+
}
40+
}
41+
42+
class B {
43+
bool cal(String str, String ans) {
44+
int n = str.length;
45+
int m = ans.length;
46+
int idx = 0;
47+
for (int i = 0; i < n; i++) {
48+
if (str[i] != ans[idx]) return false;
49+
idx++;
50+
idx %= m;
51+
}
52+
return true;
53+
}
54+
55+
String gcdOfStrings(String str1, String str2) {
56+
int n = str1.length;
57+
int m = str2.length;
58+
int ss = n.gcd(m);
59+
String ans = str1.substring(0, ss);
60+
if (cal(str1, ans) && cal(str2, ans)) return ans;
61+
return "";
62+
}
63+
}
64+
65+
class C {
66+
String gcdOfStrings(String str1, String str2) {
67+
String ans = "";
68+
for (int i = 0; i < str2.length; i++) {
69+
String cur = str2.substring(0, i + 1);
70+
List<String> curArray = str2.split(cur);
71+
bool flag = true;
72+
for (int j = 0; j < curArray.length; j++) {
73+
if (curArray[j] != "") {
74+
flag = false;
75+
break;
76+
}
77+
}
78+
if (flag) {
79+
List<String> cur2Array = str1.split(cur);
80+
for (int j = 0; j < cur2Array.length; j++) {
81+
if (cur2Array[j] != "") {
82+
flag = false;
83+
break;
84+
}
85+
}
86+
if (flag) {
87+
ans = cur;
88+
}
89+
}
90+
}
91+
return ans;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
func gcdOfStrings(str1 string, str2 string) string {
4+
if str1 == str2 {
5+
return str1
6+
}
7+
8+
if len(str2) > len(str1) {
9+
str1, str2 = str2, str1
10+
}
11+
12+
if str1[:len(str2)] != str2 {
13+
return ""
14+
}
15+
16+
return gcdOfStrings(str1[len(str2):], str2)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
### Approach
4+
5+
str1+str2 == str2+str1 if and only if str1 and str2 have a gcd .
6+
E.g. str1=abcabc, str2=abc, then str1+str2 = abcabcabc = str2+str1
7+
This(str1+str2==str2+str1) is a requirement for the strings to have a gcd. If one of them is NOT a common part then gcd is "".It means we will return empty string
8+
Proof:-str1 = mGCD, str2 = nGCD, str1 + str2 = (m + n)GCD = str2 + str1
9+
Both the strings are made of same substring added multiple times.
10+
Since they are multiples, next step is simply to find the gcd of the lengths of 2 strings e.g. gcd(6,3) = 3, (we can use gcd function to find that)and get the substring of length 3 from either str1 or str2.
11+
12+
### Code - 1
13+
14+
```dart
15+
class Solution {
16+
String gcdOfStrings(String str1, String str2) {
17+
return (str1 + str2 == str2 + str1)
18+
? str1.substring(0, str1.length.gcd(str2.length))
19+
: "";
20+
}
21+
}
22+
```
23+
24+
## Code - 2
25+
26+
```dart
27+
class Solution {
28+
bool cal(String str, String ans) {
29+
int n = str.length;
30+
int m = ans.length;
31+
int idx = 0;
32+
for (int i = 0; i < n; i++) {
33+
if (str[i] != ans[idx]) return false;
34+
idx++;
35+
idx %= m;
36+
}
37+
return true;
38+
}
39+
40+
String gcdOfStrings(String str1, String str2) {
41+
int n = str1.length;
42+
int m = str2.length;
43+
int ss = n.gcd(m);
44+
String ans = str1.substring(0, ss);
45+
if (cal(str1, ans) && cal(str2, ans)) return ans;
46+
return "";
47+
}
48+
}
49+
```
50+
51+
## Code - 3 Brute Force
52+
53+
```dart
54+
class Solution {
55+
String gcdOfStrings(String str1, String str2) {
56+
String ans = "";
57+
for (int i = 0; i < str2.length; i++) {
58+
String cur = str2.substring(0, i + 1);
59+
List<String> curArray = str2.split(cur);
60+
bool flag = true;
61+
for (int j = 0; j < curArray.length; j++) {
62+
if (curArray[j] != "") {
63+
flag = false;
64+
break;
65+
}
66+
}
67+
if (flag) {
68+
List<String> cur2Array = str1.split(cur);
69+
for (int j = 0; j < cur2Array.length; j++) {
70+
if (cur2Array[j] != "") {
71+
flag = false;
72+
break;
73+
}
74+
}
75+
if (flag) {
76+
ans = cur;
77+
}
78+
}
79+
}
80+
return ans;
81+
}
82+
}
83+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
200200
- [**460.** LFU Cache](LFUCache/lfu_cache.dart)
201201
- [**1137.** N-th Tribonacci Number](N-thTribonacciNumber/n_th_tribonacci_number.dart)
202202
- [**1626.** Best Team With No Conflicts](BestTeamWithNoConflicts/best_team_with_no_conflicts.dart)
203+
- [**1071.** Greatest Common Divisor of Strings](GreatestCommonDivisorOfStrings/greatest_common_divisor_of_strings.dart)
203204

204205
## Reach me via
205206

0 commit comments

Comments
 (0)