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));
    }
GN算法边介数计算优化指南:如何提升大规模社交网络分析效率 本文深入探讨了GN算法在大规模社交网络分析中的核心瓶颈——边介数计算,并提供了从算法优化到工程部署的完整性能提升方案。通过引入Brandes算法、并行计算、增量更新及分布式处理等策略,有效解决了传统GN算法在处理百万级网络时效率低下的问题,为社交网络社区划分等实际应用提供了高效解决方案。 阅读详情

相关推荐

Unity中5自由度机械臂运动控制与夹爪交互仿真实践

本文详细介绍了在Unity中实现5自由度机械臂运动控制与夹爪交互仿真的完整流程。从模型导入、物理组件配置到关节运动控制算法(包括逆运动学IK),再到夹爪抓取逻辑和工业分拣案例实战,提供了可落地的代码示例与性能优化方案,助力开发者快速掌握机械臂仿真核心技术。

weixin_29021757的博客 446

【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

LabVIew串口收发的一些问题

LabView串口收发实验总结

weixin_45508826的博客 2066

【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

用R语言实现“中断时间序列分析” --- R code for ITS

ITS方法可用于评估一项干预措施的效果,从水平改变和斜率改变两个角度来描述干预引起的变化。

franksui的博客 4030

yolov8涨点系列之替换幽灵卷积GhostConv

yolov8涨点系列之替换幽灵卷积GhostConv

zhang1379的博客 3567

【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

实战复盘:纷玩岛(Livelab)候补订单微信支付协议的逆向与复现

通过本次对纷玩岛候补支付接口的分析,我们实现了一个脱离 App UI 的轻量级支付发起器。这不仅验证了其API的设计逻辑(Standby -> PrePayInfo 分层设计),也为后续的自动化监控或辅助抢票工具提供了支付环节的技术验证。技术本无罪,关键在于如何使用。理解这些底层协议,有助于我们写出更健壮的代码,也能在遇到线上支付问题时快速定位是业务层(API)问题还是通道层(微信)问题。

猫敷雪 805

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

FPGA工程师面试试题集锦11~20

时钟抖动是指芯片的某一个给定点上时钟周期发生暂时性变化,也就是说时钟周期在不同的周期上可能加长或缩短。它是一个平均值为0的平均变量。

FPGA/MATLAB学习教程/源码/项目合作开发 678

windsurf中出现无法Connecting to server的解决方案

本文主要介绍了windsurf中出现无法Connecting to server的解决方案,希望能对使用windsurf的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 6万+

lc1071. Greatest Common Divisor of Strings

Greatest Common Divisor of Strings Easy 62 25 Favorite Share 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...

weixin_34204722的博客 322

StarRocks 面试题及参考答案详解(万字详解)

Broker Load的过程通常涉及到数据的解析、转换和加载,它支持多种数据格式,并且可以处理复杂的数据导入任务。一方面,压缩可以减少磁盘I/O操作,因为压缩后的数据占用的空间更小,需要读取的数据块数量也就更少。在StarRocks中,表数据可以被划分成多个分区,每个分区包含表的一部分数据。在传统的行存储模型中,数据是按行组织的,这意味着在执行分析查询时,系统需要读取并处理整行数据,即使很多时候只需要查询其中的少数几列。物化视图的更新可以是完全重算,也可以是增量更新,取决于物化视图的定义和数据变更的模式。

大模型大数据攻城狮的专栏 2084

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” 输...

小川的博客 706

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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的博客 211

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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 702

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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的博客 337

[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 206

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的博客 232

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 446

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的博客 185

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

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

qq_41758969的博客 614

[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

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要加油 559
上一篇: 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、付费专栏及课程。

余额充值