leetcode 1071. Greatest Common Divisor of Strings(字符串的最大公约数)

LeetCode --- 1071. Greatest Common Divisor of Strings 解题报告 For two stringssandt, we say "tdividess" if and only ifs = t + ... + t(i.e.,tis concatenated with itself one or more times). Given two stringsstr1andstr2, returnthe largest stringxsuch thatxdivides bothstr1andstr2. Example 1: Inpu... 阅读详情

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).

Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.

Example 1:

Input: str1 = “ABCABC”, str2 = “ABC”
Output: “ABC”

Example 2:

Input: str1 = “ABABAB”, str2 = “ABAB”
Output: “AB”

Example 3:

Input: str1 = “LEET”, str2 = “CODE”
Output: “”

字符串t 能整除字符串 s 表示 s = t + … + t, 即多个t 能拼接成 s.
求s 和 t 的最大公约数字符串。

思路:

这是个求最大公约数(gcd)的问题,
回忆一下求两个数a,b 的最大公约数 gcd(a,b) = (b == 0 ? a : gcd(b, a%b).

如果 s1 和 s2 中都只有一种字母,比如 s1 = AAAA, s2 = AA
那么公约数可以有A, AA, 其实就是gcd(s1.length, s2.length) = gcd(4, 2) = 2
最大公约数的长度是2, 也就是AA

那如果有多个字母呢?其实也是一样的,前提是它们要有公约数,
如何判断是不是有公约数?
如果有,那么s1 和 s2 都可以用公约数拼接而成,s1 和 s2谁在前谁在后都可以整除公约数,且整除的结果是一样的。
即 s1 + s2 == s2 + s1 时它们有公约数.

只要有公约数,就可以用gcd(s1.length, s2.length)来求公约数的长度,然后根据长度取出子字符串即为最大公约数。

    public String gcdOfStrings(String str1, String str2) {
        if(!(str1 + str2).equals(str2 + str1)) return "";
        
        int len = gcd(str1.length(), str2.length());
        return str1.substring(0,len);
    }

    int gcd(int a, int b) {
        return (b == 0 ? a : gcd(b, a % b));
    }
零基础微信理财通基金实操指南(2026版) 先活下来:保证不亏大钱,再想赚钱慢慢变富:年化10%,7年翻倍,这已经很好了开始比完美重要:今天先存100元到零钱通,就是最好的开始现在打开微信,进入理财通,完成第一步风险测评吧!有任何问题可以随时问,我会用最通俗的方式解释。 阅读详情

相关推荐

C++ 程序稳定运行一段时间后异常中止?vc6 运行库的bug!!!

事件查看器提示:错误应用程序 xxxxxxx.exe,版本 0.0.0.0,错误模块 MSVCRTD.DLL,版本 6.0.9782.0,错误地址 0x00012b40。这是一个通信方面的软件,需要长期运行,在压力测试时,高负荷连续运行一定天数时必定崩溃,而且都是在msvcrtd.dll中崩溃。后来在dump文件中发现调用栈在是MSVCRTD.DLL模块,跟踪代码发现是在void * __cdecl _heap_alloc_dbg(        size_t nSize,        int nBloc

qingflying的专栏 2687

【LeetCode 75】【学习笔记】字符串的最大公因子(Greatest Common Divisor of Strings)

对于字符串 s 和 t,只有在 s = t + ... + t(t 自身连接 1 次或多次)时,我们才认定 “t 能除尽 s”。 给定两个字符串 str1 和 str2 。返回 最长字符串 x,要求满足 x 能除尽 str1 且 x 能除尽 str2 。

Yo! This is Swordup. 1086

从零搭建IoT数据中台:STM32+WebSocket实现边缘设备与Web端实时双向通信

本文详细介绍了如何从零搭建基于STM32和WebSocket协议的IoT数据中台,实现边缘设备与Web端的实时双向通信。通过硬件选型、协议设计、服务器搭建及安全优化,提供了一套低延迟、高并发的企业级解决方案,适用于智能农业和工业监控等场景。

tea88的博客 755

【Leetcode】1071. Greatest Common Divisor of Strings(最大公约字符串)

For stringsSandT, we say "TdividesS" if and only ifS = T + ... + T (Tconcatenated with itself 1 or more times) Return the largest stringXsuch thatXdividesstr1andXdividesstr2. Exa...

小I的博客 636

第17篇:深入解析OpenEuler 24.03系统中的逻辑卷管理(LVM)

OpenEuler 24.03系统中的逻辑卷管理(LVM)提供了灵活的存储管理方案,通过在物理存储与文件系统间构建抽象层实现动态资源分配。

专注于系统运维、数据库、DevOps、虚拟化技术、docker、kubernetes、等云原生技术。 1705

数字后端实战指南 | Innovus Function ECO全流程解析(从LAB到Tapeout)

本文深入解析了在Cadence Innovus工具中进行Function ECO的全流程实战指南。从LAB环境搭建到Tapeout前的签核验证,详细拆解了Pre-mask与Post-mask ECO的核心步骤、关键命令(如ecoDesign)以及常见避坑方案。重点阐述了如何应对时序、DRC等挑战,并强调了备用单元管理与严格验证的重要性,是数字后端工程师提升综合能力的必备参考。

weixin_28452161的博客 650

【Leetcode】1071. Greatest Common Divisor of Strings(配数学证明)

题目地址: https://leetcode.com/problems/greatest-common-divisor-of-strings/ 给定两个字符串sss和ttt,如果存在字符串xxx使得s=x+x+...+xs=x+x+...+xs=x+x+...+x并且t=x+x+...+xt=x+x+...+xt=x+x+...+x,也就是说sss和ttt都可以分割为某个相同字符串的拼接,那就返回最长满足条件的xxx,否则返回空串。题目要求分割出xxx的出现次数要大于等于111,也就是说不能分出空串。我们可

数学、算法爱好者的博客 392

接口操作MySQL跟MongoDB事务回滚问题

通常使用的事务注解:@Transactional 是不会对MongoDB生效的,但是在一些生产接口中无法避免同时使用MySQL跟MongoDB的操作。

weixin_45932023的博客 1584

1071. Greatest Common Divisor of Strings(Leetcode每日一题-2020.03.12)

Problem 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) Return the largest string X such that X divides str1 and X divides str2. Ex...

Bryan要加油 563

UART中断接收的实战陷阱:HAL库常见误区与性能调优指南

本文深入解析STM32 HAL库UART中断接收机制,揭示常见实战陷阱如数据溢出、中断优先级配置不当等,并提供性能调优方案。重点介绍如何使用空闲中断处理不定长数据、DMA与中断混合模式优化,以及工业级错误恢复与流量控制策略,帮助开发者构建稳定可靠的串口通信系统。

pea55的博客 753

知识图谱实战应用8-从文本关系抽取到知识图谱关系构建流程贯通

大家好,我是微学AI,今天给大家介绍一下知识图谱实战应用8-从文本关系抽取到知识图谱关系构建流程贯通。我们从文本数据中采集到关键信息,并抽取出其中的关系信息,然后在存入图数据库中,整个过程实现自动化,我这里将举一个文本例子进行抽取。 对于知识图谱的构建是将实体、概念和它们之间的关系都以图表形式呈现出来的一种数据结构。要构建一个知识图谱,需要从不同的数据源中采集到相关信息,并将这些信息整合在一起,最终形成一个全面而且精确的知识图谱。

微学AI的博客 2554

845. Greatest Common Divisor

描述Given two numbers, number a and number b. Find the greatest common divisor of the given two numbers.In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero,...

vestlee的博客 902

26. 如何给 SAP BTP Cloud Foundry 环境分配运行时内存

摘要:本文解答了SAP BTP Cloud Foundry环境中Space可用内存为0MB的问题。指出Space内存配额取决于Org配额分配,其计算方式为Org总配额减去所有运行中应用实例的内存预留总和。详细解释了SAP BTP的资源分配层级(Global account→Subaccount→Environment→Org→Space),并说明如何通过Global account的Entity Assignments查看Memory Plan配额分配(以GB为单位)。最后建议遇到问题的用户检查Subacc

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1281

LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)

这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071)。对于字符串S和T,当且仅当S = T + ... + T(T与自身连接1次或更多次)时,我们说"T除S"。 返回最大的字符串X,使得X除以str1,X除以str2。 例如: 输入:str1 =“ABCABC”,str2 =“ABC” 输出:“ABC” 输...

小川的博客 707

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode-3.12-1071-E-字符串的最大公共因子/最大公约数(Greatest Common Divisor of Strings)

文章目录思路解法 对于字符串 S 和 T,只有在 S = T + … + T(T 与自身连接 1 次或多次)时,我们才认定 “T 能除尽 S”。 返回最长字符串 X,要求满足 X 能除尽 str1 且 X 能除尽 str2。 示例 1: 输入:str1 = “ABCABC”, str2 = “ABC” 输出:“ABC” 示例 2: 输入:str1 = “ABABAB”, str2 = “ABAB...

Heorin X的博客 212

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

LeetCode | 1071. Greatest Common Divisor of Strings字符串的最大公因子【Python】

LeetCode 1071. Greatest Common Divisor of Strings字符串的最大公因子【Easy】【Python】【字符串】 Problem LeetCode For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concatenated with itself 1 ...

Wonz 707

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Leetcode 1071.字符串的最大公因子(Greatest Common Divisor of Strings)

Leetcode 1071.字符串的最大公因子 1 题目描述(Leetcode题目链接)   对于字符串 SSS 和 TTT,只有在 S=T+...+TS = T + ... + TS=T+...+T(TTT 与自身连接 1 次或多次)时,我们才认定 “TTT 能除尽 SSS”。 返回最长字符串 X,要求满足 X 能除尽 str1 且 X 能除尽 str2。 输入:str1 = "ABCABC",...

qq_39378221的博客 670

【LeetCode】 1071. Greatest Common Divisor of Strings 字符串的最大公因子(Easy)(JAVA)

【LeetCode】 1071. Greatest Common Divisor of Strings 字符串的最大公因子(Easy)(JAVA) 题目地址: https://leetcode.com/problems/greatest-common-divisor-of-strings/ 题目描述: For strings S and T, we say “T divides S” if and...

qq_16927853的博客 338

[leetcode] 1071. Greatest Common Divisor of Strings

Description For two strings s and t, we say “t divides s” if and only if s = t + … + t (t concatenated with itself 1 or more times) Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2. Example 1: Input: str1

Learning from the mistakes 209

LeetCode 1071. Greatest Common Divisor of Strings C++

Problem 题目链接 Solution 题意为:给定字符串str1,str2,找出这两字符串的最大的公共循环节 例如ababab和ab的最大公共循环节是ab abababab和abab的最大公共循环节abab 那么,若str1和str2存在公共循环节,那么str1和str2的最小循环节应该是相同的 例如 abababab和abab的最小循环节都是ab,那么它们之间必定存在公共循环节 那么由最小循环节相同我们可以知道 str1+str2==str2+str1 否则肯定不存在公共循环节 那么怎么求最大公共循

dong的博客 271

leetcode 1071. Greatest Common Divisor of Strings 字符串的最大公因子 辗转相除法

leetcode 1071. Greatest Common Divisor of Strings 字符串的最大公因子 辗转相除法 leetcode 2020年3月 每日一题打卡 辗转相除法请看https://blog.csdn.net/weixin_43973433/article/details/105007066 题目: 对于字符串 S 和 T,只有在 S = T + … + T(T 与自身...

weixin_43973433的博客 233

LeetCode //C -1071. Greatest Common Divisor of Strings

This problem requires finding the largest string that divides both input strings str1 and str2, meaning both strings can be formed by repeating the divisor string. The solution involves checking if concatenating the strings in different orders produces the

Made in Code 450

leetcode 1071. Greatest Common Divisor of Strings

题目 class Solution { public String gcdOfStrings(String str1, String str2) { if(!(str1+str2).equals(str2+str1)) return ""; int len = Math.min(str1.length(),str2.length()); fo...

xiaobailaji的博客 186

算法练习-LeetCode1071. Greatest Common Divisor of Strings

算法练习-LeetCode1071. Greatest Common Divisor of Strings

qq_41758969的博客 615

[Swift]LeetCode1071.字符串的最大公因子 | Greatest Common Divisor of Strings

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30727835的博客 136

【leetcode 最大公约数 C++】1071. Greatest Common Divisor of Strings

1071. Greatest Common Divisor of Strings class Solution { public: string gcdOfStrings(string str1, string str2) { if(str1.size() < str2.size()) return gcdOfStrings(str2, str1); while(str2.size()) { string temp = str1;

izhxxx的博客 224
上一篇: leetcode 1137. N-th Tribonacci Number(第N个泰波那契数)
下一篇: leetcode 953. Verifying an Alien Dictionary(验证外星字典)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址