1071. Greatest Common Divisor of Strings

For two strings s and t, we say "t divides s" if and only if s = t + ... + t  (t concatenated with itself 1 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: ""

Example 4:

Input: str1 = "ABCDEF", str2 = "ABC"
Output: ""

 

Constraints:

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

Rust Solution

struct Solution;

impl Solution {
    fn gcd_of_strings(str1: String, str2: String) -> String {
        let n1 = str1.len();
        let n2 = str2.len();
        let mut n = usize::min(n1, n2);
        while n > 0 {
            if n1 % n != 0 || n2 % n != 0 {
                n -= 1;
                continue;
            }
            let s1 = &str1[0..n];
            let s2 = &str2[0..n];
            if s1 != s2 {
                n -= 1;
                continue;
            }
            let k1 = n1 / n;
            let k2 = n2 / n;
            if str1.matches(s1).count() == k1 && str2.matches(s2).count() == k2 {
                return s1.to_string();
            }
            n -= 1;
        }
        "".to_string()
    }
}

#[test]
fn test() {
    let str1 = "ABCABC".to_string();
    let str2 = "ABC".to_string();
    let res = "ABC".to_string();
    assert_eq!(Solution::gcd_of_strings(str1, str2), res);
    let str1 = "ABABAB".to_string();
    let str2 = "ABAB".to_string();
    let res = "AB".to_string();
    assert_eq!(Solution::gcd_of_strings(str1, str2), res);
    let str1 = "LEET".to_string();
    let str2 = "CODE".to_string();
    let res = "".to_string();
    assert_eq!(Solution::gcd_of_strings(str1, str2), res);
}

Having problems with this solution? Click here to submit an issue on github.