当前位置: 首页 > news >正文

LeetCode //C - 1071. Greatest Common Divisor of Strings

1071. 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.

From: LeetCode
Link: 1071. Greatest Common Divisor of Strings


Solution:

Ideas:
  1. Check Concatenation Equality: Concatenate str1 with str2 and str2 with str1. If these concatenated strings are not equal, there is no common divisor string and we should return an empty string.

  2. Find the GCD of the Lengths: If the strings pass the first check, find the GCD of the lengths of the two strings.

  3. Create and Return the GCD String: The GCD string is a substring of either string with length equal to the GCD of the lengths of the two strings.

Code:
// Function to find the greatest common divisor of two integers
int gcd(int a, int b) {while (b != 0) {int t = b;b = a % b;a = t;}return a;
}// Function to find the greatest common divisor of two strings
char* gcdOfStrings(char* str1, char* str2) {int len1 = strlen(str1), len2 = strlen(str2);// Allocate memory for concatenated stringschar *concat1 = (char *)malloc(sizeof(char) * (len1 + len2 + 1));char *concat2 = (char *)malloc(sizeof(char) * (len1 + len2 + 1));// Concatenate str1 and str2strcpy(concat1, str1);strcat(concat1, str2);// Concatenate str2 and str1strcpy(concat2, str2);strcat(concat2, str1);// Check if concatenated strings are equalif (strcmp(concat1, concat2) != 0) {free(concat1);free(concat2);return "";}free(concat1);free(concat2);int gcdLength = gcd(len1, len2);char *result = (char *)malloc(sizeof(char) * (gcdLength + 1));strncpy(result, str1, gcdLength);result[gcdLength] = '\0';return result;
}
http://www.lryc.cn/news/258717.html

相关文章:

  • 智能优化算法应用:基于群居蜘蛛算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • AtCoder Beginner Contest 332
  • 华为OD试题二(文件目录大小、相对开音节、找最小数)
  • 【Spark精讲】Spark作业执行原理
  • Docker容器:Centos7搭建Docker镜像私服harbor
  • ClickHouse安装和部署
  • Spring Cloud Gateway中对admin端点进行认证
  • 2. 如何通过公网IP端口映射访问到设备的vmware虚拟机的ubuntu服务器
  • 配置android sudio出现的错误
  • 【初阶C++】前言
  • MAC IDEA Maven Springboot
  • Angular13无法在浏览器debug
  • H.264与H.265(HEVC):视频编码的演进
  • Python从入门到精通九:Python异常、模块与包
  • 无需公网IP联机Minecraft,我的世界服务器本地搭建教程
  • 机器学习-SVM(支持向量机)
  • 保姆级:Windows Server 2012上安装.NET Framework 3.5
  • 昇腾910安装驱动出错,降低Centos7.6的内核版本
  • LeetCode刷题日志-73矩阵置零
  • 基于Python+WaveNet+MFCC+Tensorflow智能方言分类—深度学习算法应用(含全部工程源码)(四)
  • 文件操作及函数
  • 阿里云国际版无法远程连接Windows服务器的排查方法
  • 华清远见嵌入式学习——QT——作业4
  • Visuial Studio 打开 Unity 脚本时,脚本继承MonoBehaviour暂时失效为白色的解决方法
  • CentOS使用kkFileView实现在线预览word excel pdf等
  • 黑豹程序员-EasyExcel实现导出
  • 【项目小结】优点分析
  • 已经写完的论文怎么降低查重率 papergpt
  • 科研论文中PPT图片格式选择与转换:EPS、SVG 和 PDF 的比较
  • mybatis xml 热部署