String Similarity

Sort by

recency

|

180 Discussions

|

  • + 0 comments

    Javascript(Node.js) solution :

    function stringSimilarity(s) {
        let n = s.length;
        if (n === 0) return 0;
    
        // The similarity of the string with itself is its own length.
        // We start the sum with this value (s compared with s).
        let sum = n;
    
        // The Z-array will store the length of the longest common prefix 
        // between s and the suffix starting at i.
        let z = new Array(n).fill(0);
        
        // L and R define the interval [L, R] which is the Z-box 
        // (the segment matching the prefix of s).
        let L = 0, R = 0;
    
        for (let i = 1; i < n; i++) {
            // If i is outside the current Z-box, do a naive comparison
            if (i > R) {
                L = R = i;
                while (R < n && s[R] === s[R - L]) {
                    R++;
                }
                z[i] = R - L;
                R--;
            } else {
                // If i is inside the current Z-box, we can use previously calculated values
                let k = i - L;
                
                // Check if the value at k fits within the remaining box
                if (z[k] < R - i + 1) {
                    z[i] = z[k];
                } else {
                    // Otherwise, we need to extend the search beyond R
                    L = i;
                    while (R < n && s[R] === s[R - L]) {
                        R++;
                    }
                    z[i] = R - L;
                    R--;
                }
            }
            sum += z[i];
        }
    
    return sum;
    

    }

  • + 0 comments

    import java.util.Scanner;

    public class StringSimilarity { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); // Number of test cases scanner.nextLine(); // Consume newline

        while (t-- > 0) {
            String s = scanner.nextLine(); // Input string
            int[] z = zFunction(s);
            long sum = 0;
    
            for (int i = 0; i < s.length(); i++) {
                sum += z[i];
            }
    
            System.out.println(sum); // Output sum of similarities
        }
    
        scanner.close();
    }
    
    // Function to compute Z array for a given string
    private static int[] zFunction(String s) {
        int n = s.length();
        int[] z = new int[n];
        z[0] = n;
        int l = 0, r = 0;
    
        for (int i = 1; i < n; i++) {
            if (i <= r) {
                z[i] = Math.min(r - i + 1, z[i - l]);
            }
    
            while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) {
                z[i]++;
            }
    
            if (i + z[i] - 1 > r) {
                l = i;
                r = i + z[i] - 1;
            }
        }
    
        return z;
    }
    

    }

  • + 0 comments

    **This is the best i could do , it gives the accurate answer but it failed only the time related test case, if some one could optimize this code plz go for it **

    def stringSimilarity(s):
    # Write your code here
    c = 0
    for i in range(1,len(s)):
    a = s[i:]
    for j in range(len(a)):
    if a[j] == s[j]:
    c += 1 
    else:
    break
    return c + len(s)
    
  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    def stringSimilarity(s): n = len(s) z = [0] * n z[0] = n l, r = 0, 0

    for i in range(1, n):
        if i <= r:
            z[i] = min(r - i + 1, z[i - l])
        while i + z[i] < n and s[z[i]] == s[i + z[i]]:
            z[i] += 1
        if i + z[i] - 1 > r:
            l, r = i, i + z[i] - 1
    
    return sum(z)
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())
    
    for t_itr in range(t):
        s = input()
    
        result = stringSimilarity(s)
    
        fptr.write(str(result) + '\n')
    
    fptr.close()
    
  • + 0 comments

    import java.util.Scanner;

    public class StringSimilarity { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); // Number of test cases

        while (t-- > 0) {
            String s = scanner.next(); // Input string
            int n = s.length();
            int[] z = zFunction(s);
            long sum = 0;
    
            for (int i = 0; i < n; i++) {
                sum += z[i];
            }
    
            System.out.println(sum); // Output sum of similarities
        }
    
        scanner.close();
    }
    
    // Function to compute Z array for a given string
    private static int[] zFunction(String s) {
        int n = s.length();
        int[] z = new int[n];
        z[0] = n;
        int l = 0, r = 0;
    
        for (int i = 1; i < n; i++) {
            if (i <= r) {
                z[i] = Math.min(r - i + 1, z[i - l]);
            }
    
            while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i])) {
                z[i]++;
            }
    
            if (i + z[i] - 1 > r) {
                l = i;
                r = i + z[i] - 1;
            }
        }
    

    Read the number of test cases and input strings. For each string, compute its Z array using the Z algorithm. Sum up the values in the Z array to get the similarity. Output the similarity for each string. Repeat for each test case.

        return z;
    }
    

    }