1071:字符串的最大公因子

This commit is contained in:
huangge1199@hotmail.com 2021-05-07 23:12:39 +08:00
parent eda3db45a5
commit 9369128ab8
2 changed files with 155 additions and 0 deletions

View File

@ -0,0 +1,118 @@
//对于字符串 S T只有在 S = T + ... + TT 自身连接 1 次或多次我们才认定 T 能除尽 S
//
// 返回最长字符串 X要求满足 X 能除尽 str1 X 能除尽 str2
//
//
//
// 示例 1
//
//
//输入str1 = "ABCABC", str2 = "ABC"
//输出"ABC"
//
//
// 示例 2
//
//
//输入str1 = "ABABAB", str2 = "ABAB"
//输出"AB"
//
//
// 示例 3
//
//
//输入str1 = "LEET", str2 = "CODE"
//输出""
//
//
//
//
// 提示
//
//
// 1 <= str1.length <= 1000
// 1 <= str2.length <= 1000
// str1[i] str2[i] 为大写英文字母
//
// Related Topics 字符串
// 👍 194 👎 0
package leetcode.editor.cn;
//1071:字符串的最大公因子
public class GreatestCommonDivisorOfStrings {
public static void main(String[] args) {
//测试代码
Solution solution = new GreatestCommonDivisorOfStrings().new Solution();
solution.gcdOfStrings("TAUXXTAUXXTAUXXTAUXXTAUXX","TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
// //第一版
// public String gcdOfStrings(String str1, String str2) {
// int length1 = str1.length();
// int length2 = str2.length();
// if (length1 == length2 && !str1.equals(str2)) {
// return "";
// }
// return length1 >= length2 ? get(str1, str2) : get(str2, str1);
// }
//
// private String get(String str1, String str2) {
// int length1 = str1.length();
// int length2 = str2.length();
// if (length1 % length2 == 0) {
// return "".equals(str1.replace(str2, "")) ? str2 : "";
// }
// int index = str2.lastIndexOf(str2.charAt(0));
// String str = str2;
// do {
// str = str.substring(0, index);
// if ("".equals(str1.replace(str, "")) && "".equals(str2.replace(str, ""))) {
// return str;
// }
// index = str.lastIndexOf(str2.charAt(0));
// } while (index > 0);
// return "";
// }
public String gcdOfStrings(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
for (int i = Math.min(len1, len2); i > 0; i--) {
if (len1 % i == 0 && len2 % i == 0) {
String str = str1.substring(0, i);
if ("".equals(str1.replace(str, "")) && "".equals(str2.replace(str, ""))) {
return str;
}
}
}
return "";
}
// //官方
// public String gcdOfStrings(String str1, String str2) {
// int len1 = str1.length(), len2 = str2.length();
// for (int i = Math.min(len1, len2); i >= 1; --i) { // 从长度大的开始枚举
// if (len1 % i == 0 && len2 % i == 0) {
// String X = str1.substring(0, i);
// if (check(X, str1) && check(X, str2)) {
// return X;
// }
// }
// }
// return "";
// }
//
// public boolean check(String t, String s) {
// int lenx = s.length() / t.length();
// StringBuffer ans = new StringBuffer();
// for (int i = 1; i <= lenx; ++i) {
// ans.append(t);
// }
// return ans.toString().equals(s);
// }
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,37 @@
<p>对于字符串 <code>S</code> 和 <code>T</code>,只有在 <code>S = T + ... + T</code><code>T</code> 自身连接 1 次或多次)时,我们才认定 “<code>T</code> 能除尽 <code>S</code>”。</p>
<p>返回最长字符串 <code>X</code>,要求满足 <code>X</code> 能除尽 <code>str1</code> 且 <code>X</code> 能除尽 <code>str2</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>str1 = "ABCABC", str2 = "ABC"
<strong>输出:</strong>"ABC"
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>str1 = "ABABAB", str2 = "ABAB"
<strong>输出:</strong>"AB"
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>str1 = "LEET", str2 = "CODE"
<strong>输出:</strong>""
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ol>
<li><code>1 <= str1.length <= 1000</code></li>
<li><code>1 <= str2.length <= 1000</code></li>
<li><code>str1[i]</code> 和 <code>str2[i]</code> 为大写英文字母</li>
</ol>
<div><div>Related Topics</div><div><li>字符串</li></div></div>\n<div><li>👍 194</li><li>👎 0</li></div>