xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode Greatest Common Divisor of Strings solutions All In One

LeetCode Greatest Common Divisor of Strings solutions 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) {
        if(str1.startsWith(temp.join(``) + s)) {
          temp.push(s);
          let reg = new RegExp(temp.join(``), 'g');
          if(str1.replaceAll(reg, ``) === `` && str2.replaceAll(reg, ``) === ``) {
            // 替换,收集 ✅
            result = temp.join(``);
          }
        } else {
          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, ``) === ``) {
            // 替换,收集 ✅
            result = temp.join(``);
          }
        } else {
          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);
};
  1. 比较 string 大小 (github 上 javascript 数据结构与算法 第三版, 我曾经提过 issues ✅)

https://github.com/trekhleb/javascript-algorithms

https://github.com/loiane/javascript-datastructures-algorithms/issues/29
https://github.com/loiane/javascript-datastructures-algorithms

function gcdOfStrings(str1: string, str2: string): string {
    let largerWord: string, smallerWord: string;
    if(str1.length >= str2.length){
        largerWord = str1;
        smallerWord = str2;
    } else {
        largerWord = str2;
        smallerWord = str1
    }
    if(smallerWord === largerWord) {
        return smallerWord;
    }
    let largestCommonDenominator = '';
    for(let i = smallerWord.length; i > 0; i--){
        const possibleDenominator = smallerWord.slice(0, i);
        const possibleDenominatorLength = possibleDenominator.length;
        if(smallerWord.length % possibleDenominatorLength === 0 && largerWord.length % possibleDenominatorLength === 0) {
            const numSmaller = smallerWord.length / possibleDenominatorLength;
            const numLarger = largerWord.length / possibleDenominatorLength;
            if(possibleDenominator.repeat(numSmaller) === smallerWord && possibleDenominator.repeat(numLarger) === largerWord) {
               return possibleDenominator
            }
        }
    }
    return '';
}

demos

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;
};

image

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

https://leetcode.com/problems/greatest-common-divisor-of-strings/solutions/5195064/leetcode-1071-greatest-common-divisor-of-strings-solution/

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

LeetCode 75

Ace Coding Interview with 75 Qs

image

https://leetcode.com/studyplan/leetcode-75/

refs

https://www.cnblogs.com/xgqfrms/p/11264694.html#4311594

https://github.com/xgqfrms/23-design-patterns-app/issues/1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2024-05-23 10:42  xgqfrms  阅读(3)  评论(1编辑  收藏  举报