首页 > 其他分享 >LeetCode Greatest Common Divisor of Strings All In One

LeetCode Greatest Common Divisor of Strings All In One

时间:2024-05-23 10:40:59浏览次数:21  
标签:join Divisor temp str2 str1 reg Common return LeetCode

LeetCode Greatest Common Divisor of Strings All In One

LeetCode 1071

errors


function gcdOfStrings(str1: string, str2: string): string {
  let result = ``;
  let temp = [];
  if(str1.length > str2.length) {
    let reg = new RegExp(str2, 'g');
    if(str1.replaceAll(reg, ``) === ``) {
      return str2;
    } else {
      for(let s of str2) {
        // console.log(`s =`, s);
        if(str1.startsWith(temp.join(``) + s)) {
          temp.push(s);
          // console.log(`temp =`, temp.join(``));
          let reg = new RegExp(temp.join(``), 'g');
          // console.log(`str1 left =`, str1.replaceAll(reg, ``));
          // console.log(`str2 left =`, str2.replaceAll(reg, ``));
          if(str1.replaceAll(reg, ``) === `` && str2.replaceAll(reg, ``) === ``) {
            // 最大公约数 ✅
            // break;
            // return temp.join(``);
            // 替换,收集
            result = temp.join(``);
          }
        } else {
          // break;
          return ``;
        }
      }
    }
  } else {
    let reg = new RegExp(str1, 'g');
    if(str2.replaceAll(reg, ``) === ``) {
      return str1;
    } else {
      for(let s of str1) {
      if(str2.startsWith(temp.join(``) + s)) {
        temp.push(s);
        let reg = new RegExp(temp.join(``), 'g');
        if(str1.replaceAll(reg, ``) === `` && str2.replaceAll(reg, ``) === ``) {
          // 最大公约数 ✅
          // return temp.join(``);
          // 替换,收集
          result = temp.join(``);
        }
      } else {
        return ``;
      }
    }
    }
  }
  // return ``;
  return result;
};

// function gcdOfStrings(str1: string, str2: string): string {
//   let temp = [];
//   if(str1.length > str2.length) {
//     for(let s of str2) {
//       console.log(`s =`, s);
//       if(str1.startsWith(temp.join(``) + s)) {
//         temp.push(s);
//         console.log(`temp =`, temp.join(``));
//         let reg = new RegExp(temp.join(``), 'g');
//         console.log(`str1 left =`, str1.replaceAll(reg, ``));
//         console.log(`str2 left =`, str2.replaceAll(reg, ``));
//         if(str1.replaceAll(reg, ``) === `` && str2.replaceAll(reg, ``) === ``) {
//           // 最大公约数 ✅
//           // break;
//           return temp.join(``);
//         }
//       } else {
//         // break;
//         return ``;
//       }
//     }
//   } else {
//     for(let s of str1) {
//       if(str2.startsWith(temp.join(``) + s)) {
//         temp.push(s);
//         let reg = new RegExp(temp.join(``), 'g');
//         if(str1.replaceAll(reg, ``) === `` && str2.replaceAll(reg, ``) === ``) {
//           // 最大公约数 ✅
//           return temp.join(``);
//         }
//       } else {
//         return ``;
//       }
//     }
//   }
//   return ``;
// };

/* 

Wrong Answer
94 / 124 testcases passed

Input
str1 =
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
str2 =
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"


Output
"A"
Expected
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

 */


/* 

Wrong Answer
63 / 124 testcases passed

Input
str1 =
"ABCDEF"
str2 =
"ABC"

Output
"ABC"
Expected
""

 */

/* 

Wrong Answer
78 / 124 testcases passed

Input
str1 =
"ABABABAB"
str2 =
"ABAB"

Output
"AB"
Expected
"ABAB"
 */



solutions

function gcdOfStrings(str1: string, str2: string): string {
  let result = ``;
  let temp = [];
  if(str1.length > str2.length) {
    let reg = new RegExp(str2, 'g');
    if(str1.replaceAll(reg, ``) === ``) {
      return str2;
    } else {
      for(let s of str2) {
        // console.log(`s =`, s);
        if(str1.startsWith(temp.join(``) + s)) {
          temp.push(s);
          // console.log(`temp =`, temp.join(``));
          let reg = new RegExp(temp.join(``), 'g');
          // console.log(`str1 left =`, str1.replaceAll(reg, ``));
          // console.log(`str2 left =`, str2.replaceAll(reg, ``));
          if(str1.replaceAll(reg, ``) === `` && str2.replaceAll(reg, ``) === ``) {
            // 最大公约数 ✅
            // break;
            // return temp.join(``);
            // 替换,收集
            result = temp.join(``);
          }
        } else {
          // break;
          return ``;
        }
      }
    }
  } else {
    let reg = new RegExp(str1, 'g');
    if(str2.replaceAll(reg, ``) === ``) {
      return str1;
    } else {
      for(let s of str1) {
      if(str2.startsWith(temp.join(``) + s)) {
        temp.push(s);
        let reg = new RegExp(temp.join(``), 'g');
        if(str1.replaceAll(reg, ``) === `` && str2.replaceAll(reg, ``) === ``) {
          // 最大公约数 ✅
          // return temp.join(``);
          // 替换,收集
          result = temp.join(``);
        }
      } else {
        return ``;
      }
    }
    }
  }
  // return ``;
  return result;
};
function gcdOfStrings(str1: string, str2: string): string {
    if (str1 + str2 !== str2 + str1) return ''

    const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
    const len = gcd(str1.length, str2.length);

    return str1.substring(0, len);
};

demos

https://leetcode.com/problems/greatest-common-divisor-of-strings/?envType=study-plan-v2&envId=leetcode-75

(

标签:join,Divisor,temp,str2,str1,reg,Common,return,LeetCode
From: https://www.cnblogs.com/xgqfrms/p/18207872

相关文章

  • 【LeetCode】59. 螺旋矩阵 II
    题目:59.螺旋矩阵II解题思路手动模拟螺旋矩阵,分别实现四个方向的代码,将数组依次填入数组中即可需要注意的是,如果n为奇数,说明最后只剩下中间的一个位置,将最后一个数直接填入即可;若n为偶数,则正好能够遍历n/2遍classSolution{publicint[][]generateMatrix(intn){......
  • [LeetCode] 1863. Sum of All Subset XOR Totals
    TheXORtotalofanarrayisdefinedasthebitwiseXORofallitselements,or0ifthearrayisempty.Forexample,theXORtotalofthearray[2,5,6]is2XOR5XOR6=1.Givenanarraynums,returnthesumofallXORtotalsforeverysubsetofnums.......
  • LeetCode 1353. Maximum Number of Events That Can Be Attended
    原题链接在这里:https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/description/题目:Youaregivenanarrayof events where events[i]=[startDayi,endDayi].Everyevent i startsat startDayi andendsat endDayi.Youcanattend......
  • vscode leetcode 插件
    区域测试//@lcprcase=start//"PAYPALISHIRINGGGG"\n3\n//@lcprcase=enddiy参数用于调试不同类型的参数和函数//@lcpr-div-debug-arg-start//funName=alternateDigitSum//paramTypes=["number"]//@lcpr-div-debug-arg-endfunName:函数名称paramTypes......
  • LeetCode 918. Maximum Sum Circular Subarray
    原题链接在这里:https://leetcode.com/problems/maximum-sum-circular-subarray/description/题目:Givena circularintegerarray nums oflength n,return themaximumpossiblesumofanon-empty subarray of nums.A circulararray meanstheendofthearray......
  • 40. 组合总和 II(leetcode)
    https://leetcode.cn/problems/combination-sum-ii/description/classSolution{List<List<Integer>>res=newArrayList<>();LinkedList<Integer>path=newLinkedList<>();intsum=0;publicList<List&l......
  • LeetCode 1669. Merge In Between Linked Lists
    原题链接在这里:https://leetcode.com/problems/merge-in-between-linked-lists/description/题目:Youaregiventwolinkedlists: list1 and list2 ofsizes n and m respectively.Remove list1'snodesfromthe ath nodetothe bth node,andput list2 in......
  • LeetCode 2595. Number of Even and Odd Bits
    原题链接在这里:https://leetcode.com/problems/number-of-even-and-odd-bits/description/题目:Youaregivena positive integer n.Let even denotethenumberofevenindicesinthebinaryrepresentationof n (0-indexed)withvalue 1.Let odd denotethen......
  • D. Deleting Divisors
    https://codeforces.com/contest/1537/problem/D题意:给定数n,alice和bob博弈,alice先手。每次操作可以减去当前n的一个因子,并且该因子不能为n和1。问谁必胜。思路:策略分析。基础分析:如果n是奇数,那么没有偶数因子。如果n是偶数,可能有偶数因子和奇数因子,如果只有偶数因子,n是2的整数......
  • 原创->CommonsCollections1-DefaultMap链
    今天我打算整点儿不一样的内容,通过之前学习的TransformerMap和LazyMap链,想搞点不一样的,所以我关注了另外一条链DefaultedMap链,主要调用链为:调用链详细描述:ObjectInputStream.readObject()DefaultedMap.readObject()DefaultedMap.get()ChainedTran......