String Similarity

  • + 1 comment

    woking solution in RUST :

    fn string_similarity(s: &str) -> i64 {
        let v = s.as_bytes();
        let len = v.len();
        let mut n: usize = 1;
        let mut sum: i64 = len as i64;
    
        for i in 1..len {
            if v[0] == v[i] {
                sum += i as i64;
            } else {
                n = i;
                break;
            }
        }
    
        if n != len - 1 {
            for i in n..len {
                let (mut p, mut q) = (i, 0);
                while p < len && v[q] == v[p] {
                    sum += 1;
                    q += 1;
                    p += 1;
                }
            }
        }
        sum
    }