首页 > 其他分享 >leetcode-1071-easy

leetcode-1071-easy

时间:2023-03-11 13:46:12浏览次数:31  
标签:return String str2 str1 1071 length easy sb leetcode

Greatest Common Divisor of Strings

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.

思路一:先求两字符串长度的最大公约数,然后依次对比最大公约数长度的子字符串是否与两字符串互相匹配

    public static String gcdOfStrings(String str1, String str2) {
        int gcd = gcd(str1.length(), str2.length());

        for (int i = gcd; i >= 1; i--) {
            String temp = str1.substring(0, i);
            if (tempEquals(temp, str1, str2)) {
                return temp;
            }
        }

        return "";
    }

    private static boolean tempEquals(String temp, String str1, String str2) {
        StringBuilder sb = new StringBuilder();
        while (sb.length() < str1.length()) {
            sb.append(temp);
        }

        if (!sb.toString().equals(str1)) {
            return false;
        }

        sb = new StringBuilder();

        while (sb.length() < str2.length()) {
            sb.append(temp);
        }

        return sb.toString().equals(str2);
    }

    private static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }

标签:return,String,str2,str1,1071,length,easy,sb,leetcode
From: https://www.cnblogs.com/iyiluo/p/17205775.html

相关文章

  • leetcode-1389-easy
    CreateTargetArrayintheGivenOrderGiventwoarraysofintegersnumsandindex.Yourtaskistocreatetargetarrayunderthefollowingrules:Initiallyt......
  • leetcode-1544-easy
    MakeTheStringGreatGivenastringsofloweranduppercaseEnglishletters.Agoodstringisastringwhichdoesn'thavetwoadjacentcharacterss[i]and......
  • leetcode-977-easy
    SquaresofaSortedArrayGivenanintegerarraynumssortedinnon-decreasingorder,returnanarrayofthesquaresofeachnumbersortedinnon-decreasingor......
  • leetcode-1021-easy
    RemoveOutermostParenthesesAvalidparenthesesstringiseitherempty"","("+A+")",orA+B,whereAandBarevalidparenthesesstrings,and+represe......
  • leetcode-1496-easy
    PathCrossingGivenastringpath,wherepath[i]='N','S','E'or'W',eachrepresentingmovingoneunitnorth,south,east,orwest,respectively.Youstart......
  • leetcode-1523-easy
    CountOddNumbersinanIntervalRangeGiventwonon-negativeintegerslowandhigh.Returnthecountofoddnumbersbetweenlowandhigh(inclusive).Example......
  • LeetCode|2457. 美丽整数的最小增量
    题目链接:2457.美丽整数的最小增量给你两个正整数n和target。如果某个整数每一位上的数字相加小于或等于target,则认为这个整数是一个美丽整数。找出并返回满......
  • 【LeetCode回溯算法#07】子集问题I+II,巩固解题模板并详解回溯算法中的去重问题
    子集力扣题目链接给你一个整数数组nums,数组中的元素互不相同。返回该数组所有可能的子集(幂集)。解集不能包含重复的子集。你可以按任意顺序返回解集。示例1:输......
  • eNSP的使用4  NAT网络地址转换 easyip
    静态转换:一对一绑定 (双向通信)路由器靠地址区分内部主机easy ip :多对一  (单向通信)路由器靠端口(临时端口)区分内部主机easy ip的配置:1.配置aclacl  2000rule perm......
  • easyexcel填坑-校验表头为空,或者不符合预期
    背景:easyexcelv3.1.5实体类已经使用注解@ExcelProperty标注需要导入的属性正文开始关闭忽略空行,防止第一行是空跳过校验 ignoreEmptyRow(false)。此处如果未关闭,第......