首页 > 其他分享 >[LeetCode] 1071. Greatest Common Divisor of Strings

[LeetCode] 1071. Greatest Common Divisor of Strings

时间:2023-06-27 14:57:37浏览次数:49  
标签:return Divisor int str2 str1 Common 字符串 LeetCode

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: ""

Constraints:

  • 1 <= str1.length, str2.length <= 1000
  • str1 and str2 consist of English uppercase letters.

字符串的最大公因子。

对于字符串 s 和 t,只有在 s = t + ... + t(t 自身连接 1 次或多次)时,我们才认定 “t 能除尽 s”。

给定两个字符串 str1 和 str2 。返回 最长字符串 x,要求满足 x 能除尽 str1 且 x 能除尽 str2 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/greatest-common-divisor-of-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道数学题,说是求字符串的最大公因子,其实是需要先求出两个字符串长度的最大公约数然后得到子串。其中求最大公约数 gcd 的函数需要背下来。

注意题目的 corner case,如果两个字符串 A + B != B + A 的话,说明两者是没有公共子串的。

时间O(log min(a, b))

空间O(1)

Java实现

 1 class Solution {
 2     public String gcdOfStrings(String str1, String str2) {
 3         // corner case
 4         if (!str1.concat(str2).equals(str2.concat(str1))) {
 5             return "";
 6         }
 7         int gcdLength = helper(str1.length(), str2.length());
 8         return str1.substring(0, gcdLength);
 9     }
10     
11     private int helper(int a, int b) {
12         while (b != 0) {
13             int temp = b;
14             b = a % b;
15             a = temp;
16         }
17         return a;
18     }
19 }

 

LeetCode 题目总结

标签:return,Divisor,int,str2,str1,Common,字符串,LeetCode
From: https://www.cnblogs.com/cnoodle/p/17508851.html

相关文章

  • SAP UI5 命名空间 com.sap.vocabularies.Common.v1 的作用
    SAPUI5是一种基于JavaScript的用户界面技术,用于构建企业级Web应用程序。它提供了一套丰富的控件库,可以帮助开发者轻松地创建响应式、跨平台的用户界面。命名空间是一种在编程中常见的概念,用于区分不同的代码库或功能模块,以避免命名冲突。com.sap.vocabularies.Common.v1是......
  • LeetCode —— 滑动窗口
    904. 水果成篮用一个Map记录当前窗口的情况: key-水果种类数value-这个水果种类在当前滑动窗口里出现的次数维持一个left指针到right指针的滑动窗口每次right右移一位,将新加入窗口的  fruits[right]这个种类放到map里,并将该种类计数+1如果当前窗口水果......
  • [LeetCode] 2485. Find the Pivot Integer
    Givenapositiveinteger n,findthe pivotinteger x suchthat:Thesumofallelementsbetween 1 and x inclusivelyequalsthesumofallelementsbetween x and n inclusively.Return thepivotinteger x.Ifnosuchintegerexists,return -1.......
  • LeetCode 128. 最长连续序列
    为什么这题我都不会,脑袋有点累,状态真差classSolution{public:intlongestConsecutive(vector<int>&nums){unordered_set<int>s(nums.begin(),nums.end());//记录数字是否出现过intres=0;for(autoi:nums)//枚举每个数字,查看以当前数字......
  • leetcode-前缀和数组&差分数组
    前缀和数组:前缀和技巧适用于快速、频繁地计算一个索引区间内的元素之和。(仅仅适用于原数组不变的情况,如果原数组经常修改,则需要考虑差分数组。)模版如下:classPrefixSum{//前缀和数组privateint[]preSum;/*输入一个数组,构造前缀和*/publicPrefixSu......
  • Windows Common Log File System (CLFS) Driver,也称为CLFS.sys,是Windows操作系统中的
    WindowsCommonLogFileSystem(CLFS)Driver,也称为CLFS.sys,是Windows操作系统中的一个驱动程序。它提供了一个通用的日志文件系统框架,用于记录和管理系统、应用程序和服务的日志。CLFS.sys文件的路径通常位于Windows操作系统的系统目录中。具体的路径取决于安装Windows的......
  • 二叉树-快排-leetcode912
    给你一个整数数组nums,请你将该数组升序排列。示例1:输入:nums=[5,2,3,1]输出:[1,2,3,5]示例2:输入:nums=[5,1,1,2,0,0]输出:[0,0,1,1,2,5]提示:1<=nums.length<=5*104-5*104<=nums[i]<=5*104思路:快排,或者叫前序二叉树,排序后端结果是一个二叉搜索树//lee......
  • leetcode 48 旋转图像 rotate-image【ct】
    ====思路:1.对角线翻折  i=0;i<matrix.lengthj=i;j<matrix.lengthmatrix[i][j]matrix[j][i]=matrix[j][i]matrix[i][j]2.左右翻折i=0i<matrix.lengthj=0j<Math.floor(matrix.length/2)matrix[i][j]matrix[i][matrix.lengt......
  • 【LeetCode摩尔投票】有趣的简单题:数组中出现次数超过一半的数字
    数组中出现次数超过一半的数字https://leetcode.cn/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。你可以假设数组是非空的,并且给定的数组总是存在多数元素。示例1:输入:[1,2,3,......
  • #yyds干货盘点# LeetCode程序员面试金典:各位相加
    1.简述:给定一个非负整数num,反复将各个位上的数字相加,直到结果为一位数。返回这个结果。 示例1:输入:num=38输出:2解释:各位相加的过程为:38-->3+8-->1111-->1+1-->2由于 2是一位数,所以返回2。示例2:输入:num=0输出:02.代码实现:classSolution{pu......