Given a string S, check if it is possible to convert it into a string that is the repetition of a substring with K characters. To convert, we can replace one substring of length K starting at index i (zero-based indexing), such that i is divisible by K, with K characters.
Example 1:
Input:
N = 4, K = 2, S = "bdac"
Output: 1
Explanation: We can replace either
"bd" with "ac" or "ac" with "bd"
Example 2:
Input: 
N = 5, K = 2, S = "abcde"
Output: 0
Explanation: Since N % K != 0, it's not 
possible to convert S into a string which
is a concatanation of a substring with 
length K.
Your Task:
You don't need to read input or print anything. Your task is to complete the function kSubstrConcat() which takes a string S, its length N and an integer K as inputs and return 1/0 accordingly.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
2 <= K < N <= 100000

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

	def kSubstrConcat(self, n, s, k):

		# Your Code Here

Solutions:
class Solution:

	def kSubstrConcat(self, n, s, k):
		if n % k != 0:
			return 0
		pat = s[0:k]
		(cnt, cnt2) = (0, 0)
		pat2 = ''
		for i in range(0, n, k):
			if pat == s[i:i + k]:
				cnt += 1
			elif pat2 == '':
				pat2 = s[i:i + k]
				cnt2 += 1
			elif pat2 == s[i:i + k]:
				cnt2 += 1
		if (cnt <= 1 or cnt2 <= 1) and cnt + cnt2 == n // k:
			return 1
		return 0


class Solution:

	def kSubstrConcat(self, n, s, k):
		if n % k:
			return 0
		mp = dict()
		count = 0
		for u in range(0, n, k):
			ans = ''
			for j in range(k):
				ans += s[u + j]
			if ans not in mp:
				mp[ans] = 1
				count += 1
			else:
				mp[ans] += 1
		if count == 1:
			return 1
		if count > 2:
			return 0
		key_first_value = mp[next(iter(mp))]
		return 1 if key_first_value == 1 or key_first_value == n // k - 1 else 0


class Solution:

	def kSubstrConcat(self, n, s, k):
		if n % k != 0:
			return 0
		elif n // k == 2:
			return 1
		else:
			if s[:k] == s[k:2 * k]:
				sub = s[:k]
			elif s[:k] == s[2 * k:3 * k] or s[k:2 * k] == s[2 * k:3 * k]:
				sub = s[2 * k:3 * k]
			else:
				return 0
			x = n // k
			if x == 3:
				return 1
			else:
				i = 3
				while i < x:
					if sub != s[i * k:(i + 1) * k]:
						return 0
					i += 1
		return 1


class Solution:

	def kSubstrConcat(self, n, s, k):
		if n % k != 0:
			return 0
		mp = {}
		for i in range(0, n, k):
			if s[i:i + k] not in mp:
				mp[s[i:i + k]] = 1
			else:
				mp[s[i:i + k]] = mp[s[i:i + k]] + 1
		if len(mp) == 1:
			return 1
		if len(mp) != 2:
			return 0
		for i in mp:
			if mp[i] == 1 or mp[i] == n / k - 1:
				return 1
		return 0


class Solution:

	def kSubstrConcat(self, n, s, k):
		if n == 1:
			return 1
		if n % k:
			return 0
		m = {}
		for i in range(0, n, k):
			if s[i:i + k] not in m:
				m[s[i:i + k]] = 0
			m[s[i:i + k]] += 1
		if len(m) == 1:
			return 1
		if len(m) != 2:
			return 0
		v = list(m.values())[0]
		if v == n // k - 1 or v == 1:
			return 1
		return 0


class Solution:

	def kSubstrConcat(self, n, s, k):
		mp = {}
		for i in range(0, n, k):
			mp[s[i:i + k]] = mp.get(s[i:i + k], 0) + 1
		c = 0
		if n % k != 0:
			c = 1
			return 0
		elif len(mp) == 1:
			return 1
		elif len(mp) > 2:
			c = 1
			return 0
		for i in mp:
			if mp[i] == n // k - 1 or mp[i] == 1:
				pass
			else:
				c = 1
				return 0
		if c == 0:
			return 1


class Solution:

	def kSubstrConcat(self, n, s, k):
		res = [s[y - k:y] for y in range(k, n + k, k)]
		mp = dict.fromkeys(res, 0)
		c = 0
		for i in res:
			mp[i] += 1
		if n % k != 0:
			c = 1
			return 0
		elif len(mp) == 1:
			return 1
		elif len(mp) > 2:
			c = 1
			return 0
		for i in mp:
			if mp[i] == n // k - 1 or mp[i] == 1:
				pass
			else:
				c = 1
				return 0
		if c == 0:
			return 1


class Solution:

	def kSubstrConcat(self, n, s, k):
		if n % k != 0:
			return 0
		mp = {}
		for i in range(0, n, k):
			mp[s[i:i + k]] = mp.get(s[i:i + k], 0) + 1
		if len(mp) == 1:
			return 1
		if len(mp) > 2:
			return 0
		for i in mp:
			if mp[i] == 1:
				return 1
		return 0