Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec87dec

Browse files
add 1071
1 parentcf752f5 commitec87dec

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+
packagecom.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+
publicclass_1071 {
28+
publicstaticclassSolution1 {
29+
publicStringgcdOfStrings(Stringstr1,Stringstr2) {
30+
if (str1.isEmpty() ||str2.isEmpty()) {
31+
return"";
32+
}
33+
StringcommomDivisor =str2;
34+
while (commomDivisor !=null && !commomDivisor.isEmpty()) {
35+
if (isDivisor(str1,commomDivisor)) {
36+
returncommomDivisor;
37+
}else {
38+
commomDivisor =findNextShorterCommonDivisor(str2,commomDivisor);
39+
}
40+
}
41+
return"";
42+
}
43+
44+
privateStringfindNextShorterCommonDivisor(Stringstr2,StringcommomDivisor) {
45+
intlength =nextPossibleLength(str2,commomDivisor.length() -1);
46+
while (length >1) {
47+
if (isDivisor(str2,length)) {
48+
returnstr2.substring(0,length);
49+
}
50+
length =nextPossibleLength(str2,length -1);
51+
}
52+
returnnull;
53+
}
54+
55+
privatebooleanisDivisor(Stringstr2,intlength) {
56+
StringcommonDivisorCandidate =str2.substring(0,length);
57+
for (inti =0;i <str2.length() -length;i +=length) {
58+
if (!str2.substring(i,i +length).equals(commonDivisorCandidate)) {
59+
returnfalse;
60+
}
61+
}
62+
returntrue;
63+
}
64+
65+
privateintnextPossibleLength(Stringstr2,intbound) {
66+
if (bound <=0) {
67+
return -1;
68+
}
69+
intlen =bound;
70+
while (str2.length() %len !=0) {
71+
len--;
72+
}
73+
returnlen;
74+
}
75+
76+
privatebooleanisDivisor(Stringstr1,StringcommomDivisor) {
77+
if (str1.length() ==commomDivisor.length()) {
78+
returnstr1.equals(commomDivisor);
79+
}
80+
inti =0;
81+
for (;i <str1.length() -commomDivisor.length();i +=commomDivisor.length()) {
82+
if (!str1.substring(i,i +commomDivisor.length()).equals(commomDivisor)) {
83+
returnfalse;
84+
}
85+
}
86+
returni == (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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1071;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1071Test {
10+
privatestatic_1071.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1071.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals("ABC",solution1.gcdOfStrings("ABCABC","ABC"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals("AB",solution1.gcdOfStrings("ABABAB","ABAB"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals("",solution1.gcdOfStrings("LEET","CODE"));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals("",solution1.gcdOfStrings("ABCABCD","ABC"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp