Let's say take 2 strings, A and B, and define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of strings `abc` and `abd` is 2, while the similarity of strings `aaa` and `aaab` is 3.

write a function that calculates the sum of similarities of a string S with each of it's **suffixes**.

```python
string_suffix('ababaa') => returns 11
string_suffix('abc') => returns 3
```

Explanation:

In the first case, the suffixes of the string are `ababaa`, `babaa`, `abaa`, `baa`, `aa` and `a`. The similarities of each of these strings with the string `ababaa` are 6,0,3,0,1,1 respectively. Thus the answer is 6 + 0 + 3 + 0 + 1 + 1 = 11.

For the second case, the answer is simply 3 + 0 + 0 = 3.

Note : Each string will have at least one character - no need to check for empty strings :)

Source: codewars | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: def string_suffix(s):
	

Solutions:
from os.path import commonprefix

def string_suffix(s):
	return sum((len(commonprefix([s, s[i:]])) for i in range(len(s))))


def similarity(strng1, strng2):
	result = 0
	for (c1, c2) in zip(strng1, strng2):
		if c1 != c2:
			break
		result += 1
	return result

def string_suffix(strng):
	return sum((similarity(strng[i:], strng) for i in range(len(strng))))


def string_suffix(s):
	length = len(s)
	counter = 0
	for i in range(length):
		for (i, c) in enumerate(s[i:]):
			if c == s[i]:
				counter += 1
			else:
				break
	return counter


def string_suffix(s):
	total = len(s)
	suffixes = [s[y:] for y in range(len(s)) if s[y] == s[0]][1:]
	for suff in suffixes:
		for (y, l) in zip(s, suff):
			if l == y:
				total += 1
			else:
				break
	return total


def string_suffix(s):
	sim = lambda s1, s2: ([c1 == c2 for (c1, c2) in zip(s1, s2)] + [False]).index(False)
	return sum((sim(s, s[i:]) for i in range(len(s))))


string_suffix = lambda s: sum((s[:j - i] == s[i:j] for j in range(1, len(s) + 1) for i in range(j)))


def string_suffix(str_):
	return sum((next((c for (c, (a, b)) in enumerate(zip(str_, str_[d:])) if a != b), len(str_[d:])) for d in range(len(str_))))


def string_suffix(str_):
	return sum((next((j for (j, (a, b)) in enumerate(zip(str_[i:], str_)) if a != b), len(str_) - i) for i in range(len(str_))))