Skip to content

Commit ec87dec

Browse files
add 1071
1 parent cf752f5 commit ec87dec

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java) | O(m*n) | O(1) | |Easy||
3031
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | O(nlogn) | O(1) | |Medium||
3132
|1056|[Confusing Number](https://leetcode.com/problems/confusing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1056.java) | O(n) | O(1) | |Easy||
3233
|1055|[Fixed Point](https://leetcode.com/problems/fixed-point/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1055.java) | O(n) | O(1) | |Easy||
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1071. Greatest Common Divisor of Strings
5+
*
6+
* For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concatenated with itself 1 or more times)
7+
* Return the largest string X such that X divides str1 and X divides str2.
8+
*
9+
* Example 1:
10+
* Input: str1 = "ABCABC", str2 = "ABC"
11+
* Output: "ABC"
12+
*
13+
* Example 2:
14+
* Input: str1 = "ABABAB", str2 = "ABAB"
15+
* Output: "AB"
16+
*
17+
* Example 3:
18+
* Input: str1 = "LEET", str2 = "CODE"
19+
* Output: ""
20+
*
21+
* Note:
22+
*
23+
* 1 <= str1.length <= 1000
24+
* 1 <= str2.length <= 1000
25+
* str1[i] and str2[i] are English uppercase letters.
26+
* */
27+
public class _1071 {
28+
public static class Solution1 {
29+
public String gcdOfStrings(String str1, String str2) {
30+
if (str1.isEmpty() || str2.isEmpty()) {
31+
return "";
32+
}
33+
String commomDivisor = str2;
34+
while (commomDivisor != null && !commomDivisor.isEmpty()) {
35+
if (isDivisor(str1, commomDivisor)) {
36+
return commomDivisor;
37+
} else {
38+
commomDivisor = findNextShorterCommonDivisor(str2, commomDivisor);
39+
}
40+
}
41+
return "";
42+
}
43+
44+
private String findNextShorterCommonDivisor(String str2, String commomDivisor) {
45+
int length = nextPossibleLength(str2, commomDivisor.length() - 1);
46+
while (length > 1) {
47+
if (isDivisor(str2, length)) {
48+
return str2.substring(0, length);
49+
}
50+
length = nextPossibleLength(str2, length - 1);
51+
}
52+
return null;
53+
}
54+
55+
private boolean isDivisor(String str2, int length) {
56+
String commonDivisorCandidate = str2.substring(0, length);
57+
for (int i = 0; i < str2.length() - length; i += length) {
58+
if (!str2.substring(i, i + length).equals(commonDivisorCandidate)) {
59+
return false;
60+
}
61+
}
62+
return true;
63+
}
64+
65+
private int nextPossibleLength(String str2, int bound) {
66+
if (bound <= 0) {
67+
return -1;
68+
}
69+
int len = bound;
70+
while (str2.length() % len != 0) {
71+
len--;
72+
}
73+
return len;
74+
}
75+
76+
private boolean isDivisor(String str1, String commomDivisor) {
77+
if (str1.length() == commomDivisor.length()) {
78+
return str1.equals(commomDivisor);
79+
}
80+
int i = 0;
81+
for (; i < str1.length() - commomDivisor.length(); i += commomDivisor.length()) {
82+
if (!str1.substring(i, i + commomDivisor.length()).equals(commomDivisor)) {
83+
return false;
84+
}
85+
}
86+
return i == (str1.length() - commomDivisor.length());
87+
}
88+
89+
}
90+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1071;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1071Test {
10+
private static _1071.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1071.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("ABC", solution1.gcdOfStrings("ABCABC", "ABC"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("AB", solution1.gcdOfStrings("ABABAB", "ABAB"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("", solution1.gcdOfStrings("LEET", "CODE"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("", solution1.gcdOfStrings("ABCABCD", "ABC"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy