use crate::string::z_function::z_function;

pub fn search_period(src: &str) ->&str {
    for (idx, value) in z_function(src).iter().enumerate() {
        if value + idx == src.len() && src.len() % idx == 0 {
            return &src[..idx];
        } else if value + idx == src.len() {
            let k = src.len() % idx;
            if src[..k] == src[src.len() - k..] {
                return &src[..idx];
            }
        }
    }
    src
}

#[cfg(test)]
#[test]
fn test_minimum_string_period() {
    assert_eq!(search_period("abcabcabca"), "abc");
    assert_eq!(search_period("abcdefg"), "abcdefg");
    assert_eq!(search_period("abcabcabcd"), "abcabcabcd");
    assert_eq!(search_period(""), "");
}