Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8620789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:39:52+00:00 2026-06-12T06:39:52+00:00

I am trying to solve this challenge on InterviewStreet: https://www.interviewstreet.com/challenges/dashboard/#problem/4edb8abd7cacd I already have a

  • 0

I am trying to solve this challenge on InterviewStreet: https://www.interviewstreet.com/challenges/dashboard/#problem/4edb8abd7cacd

I already have a working algorithm but I would to improve its performance. Do you have any suggestions how to do so?

# Enter your code here. Read input from STDIN. Print output to STDOUT
N = gets.to_i
words = []

while words.length < N do
  words << gets.sub(/\\n$/, '').strip
end 

words.each do |word|
  count = 0
  (word.length).times do |i|
    sub = word[i..-1]
    j=0
    while j < sub.length && sub[j] == word[j] do
      count += 1 
      j+=1
    end
  end
  puts count
end

Thanks,
Greg

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:39:53+00:00Added an answer on June 12, 2026 at 6:39 am

    Your algorithm is in the worst case quadratic. For most normal words, there is no quadratic behaviour, and it works well enough (due to its simplicity, it runs probably faster than more sophisticated algorithms with better worst-case behaviour).

    One algorithm with linear worst-case behaviour is the Z-algorithm. I don’t speak much ruby, so for the time being, the Python version will have to do:

    def zarray(str):
        Z = [0]*len(str)
        Z[0] = len(str)
        left, right, i = 0, 0, 1
        while i < len(str):
            if i > right:
                j, k = 0, i
                while k < len(str) and str[j] == str[k]:
                    j += 1
                    k += 1
                Z[i] = j
                if j > 0:
                    left, right = i, i+j-1
            else:
                z = Z[i-left]
                s = right-i+1
                if z < s:
                    Z[i] = z
                else:
                    j, k = s, s+i
                    while k < len(str) and str[j] == str[k]:
                        j += 1
                        k += 1
                    Z[i] = j
                    left, right = i, i+j-1
            i += 1
        return Z
    
    def similarity(s):
        return sum(zarray(s))
    

    Explanation of the algorithm:

    The idea is simple (but, like most good ideas, not easy to have). Let us call a (non-empty) substring that is also a prefix of the string a prefix-substring. To avoid recomputation, the algorithm uses a window of the prefix-substring starting before the currently considered index that extends farthest to the right (initially, the window is empty).

    Variables used and invariants of the algorithm:

    • i, the index under consideration, starts at 1 (for 0-based indexing; the entire string is not considered) and is incremented to length - 1
    • left and right, the first and last index of the prefix-substring window; invariants:
      1. left < i, left <= right < length(S), either left > 0 or right < 1,
      2. if left > 0, then S[left .. right] is the maximal common prefix of S and S[left .. ],
      3. if 1 <= j < i and S[j .. k] is a prefix of S, then k <= right
    • An array Z, invariant: for 1 <= k < i, Z[k] contains the length of the longest common prefix of S[k .. ] and S.

    The algorithm:

    1. Set i = 1, left = right = 0 (any values with left <= right < 1 are allowed), and set Z[j] = 0 for all indices 1 <= j < length(S).
    2. If i == length(S), stop.
    3. If i > right, find the length l of the longest common prefix of S and S[i .. ], store it in Z[i]. If l > 0 we have found a window extending farther right than the previous, then set left = i and right = i+l-1, otherwise leave them unchanged. Increment i and go to 2.
    4. Here left < i <= right, so the substring S[i .. right] is known – since S[left .. right] is a prefix of S, it is equal to S[i-left .. right-left].

      Now consider the longest common prefix of S with the substring starting at index i - left.
      Its length is Z[i-left], hence S[k] = S[i-left + k] for 0 <= k < Z[i-left] and
      S[Z[i-left]] ≠ S[i-left+Z[i-left]]. Now, if Z[i-left] <= right-i, then i + Z[i-left] is inside the known window, therefore

      S[i + Z[i-left]] = S[i-left + Z[i-left]] ≠ S[Z[i-left]]
      S[i + k]         = S[i-left + k]         = S[k]   for 0 <= k < Z[i-left]
      

      and we see that the length of the longest common prefix of S and S[i .. ] has length Z[i-left].
      Then set Z[i] = Z[i-left], increment i, and go to 2.

      Otherwise, S[i .. right] is a prefix of S and we check how far it extends, starting the comparison of characters at the indices right+1 and right+1 - i. Let the length be l. Set Z[i] = l, left = i, right = i + l - 1, increment i, and go to 2.

    Since the window never moves left, and the comparisons always start after the end of the window, each character in the string is compared at most once successfully to an earlier character in the string, and for each starting index, there is at most one unsuccessful comparison, therefore the algorithm is linear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,
I was trying to solve this problem on SPOJ (http://www.spoj.pl/problems/REC/) F(n) = a*F(n-1) +
I am trying to solve this problem. I have a series of SELECT statements
I have been trying to solve this problem for a while, could not find
I have been trying to solve this problem all day, I googled a lot,
I have been trying to solve this problem all day but haven't got any
i am trying to solve this spoj problem http://www.spoj.pl/problems/ZUMA i am unable to find
Trying to solve this problem: I have the following set of divs that when
I'm trying to solve this problem where I have a unique array of values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.