Given a string S, consisting only of ‘0’ and ‘1’, and also you are given an integer k. You are asked to find the sum of all unique substrings (in decimal notation) of length k in the string, such that string S is the repetition of these substrings. 
 
Example 1:
Input:
010100010010 3
Output:
 6
Exaplanation:
Unique substrings are “010” and “100”.
The required sum is 2+4=6.
Example 2:
Input:
00100110 4
Output:
8
 
Your Task:
You don't need to read or print anything. Your task is to complete the function unique_substring_sum() which takes the binary string S as first parameter and k as the second parameter and returns the sum of unique substring (In decimal notation).If the string S can’t be split into substrings of length k without leaving any characters in the string return -1.
 
Expected Time Complexity:  O(n)
Expected Space Complexity: O(n)
 
Constraints:
1 <= length of string <= 1000
k will not exceed 20

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3


class Solution:
	def unique_substring_sum(self, s, k):
		# Code here

Solutions:
class Solution:

	def unique_substring_sum(self, s, k):
		if len(s) % k != 0:
			return -1
		sett = set()
		while s:
			sett.add(s[:k])
			s = s[k:]
		summ = 0
		for i in sett:
			summ += int(i, 2)
		return summ


class Solution:

	def unique_substring_sum(self, s, k):
		if len(s) % k:
			return -1
		setti = set()
		while s:
			setti.add(s[:k])
			s = s[k:]
		summa = 0
		for item in setti:
			summa += int(item, 2)
		return summa


class Solution:

	def unique_substring_sum(self, s, k):
		if len(s) % k != 0:
			return -1
		a = []
		for i in range(0, len(s), k):
			a.append(int(s[i:i + k], 2))
		k = sum(set(a))
		return k


class Solution:

	def unique_substring_sum(self, s, k):
		i = 0
		m = {}
		n = len(s)
		if n % k != 0:
			return -1
		while i + k <= n:
			m[s[i:i + k]] = 1
			i = i + k
		s = 0
		for i in m:
			s += int(i, 2)
		return s


class Solution:

	def getDecimal(self, binary, k):
		j = 0
		dec = 0
		for i in range(k - 1, -1, -1):
			dec += int(binary[i]) * 2 ** j
			j += 1
		return dec

	def unique_substring_sum(self, s, k):
		lst = []
		n = len(s)
		t = k - 1
		total = 0
		if len(s) % k != 0:
			return -1
		for i in range(0, n - t, k):
			temp = s[i:i + k]
			if temp not in lst:
				lst.append(temp)
		for i in lst:
			total += self.getDecimal(i, k)
		return total


class Solution:

	def unique_substring_sum(self, s, k):
		L = []
		n = 0
		if len(s) % k != 0:
			return -1
		else:
			for i in range(0, len(s), k):
				a = s[i:k + i]
				if a not in L:
					L.append(a)
					n = n + int(a, 2)
			return n


class Solution:

	def unique_substring_sum(self, s, k):
		if len(s) % k != 0:
			return -1
		uniqueSub = set()
		steps = len(s) // k
		j = 0
		for i in range(steps):
			uniqueSub.add(s[j:j + k])
			j += k
		sum = 0
		for i in uniqueSub:
			sum += int(i, 2)
		return sum


class Solution:

	def unique_substring_sum(self, s, k):
		(x, n) = (0, len(s))
		ans = 0
		st = set()
		if n % k != 0:
			return -1
		for i in range(int(n // k)):
			snew = s[x:x + k]
			if snew not in st:
				ans += int(snew, 2)
			st.add(snew)
			x += k
		return ans