Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.


Example:

Input: s = "abcdefg", k = 2
Output: "bacdfeg"



Restrictions: 

 The string consists of lower English letters only.
 Length of the given string and k will in the range [1, 10000]

Difficulty: EASY
Tags: String algorithms

Amortized analysis
Skill types: Amortized analysis

Starter code:
class Solution:
    def reverseStr(self, s: str, k: int) -> str:
        

Solutions:
class Solution:

	def reverseStr(self, s, k):
		for idx in range(0, len(s), 2 * k):
			s = s[:idx] + s[idx:idx + k][::-1] + s[idx + k:]
		return s


class Solution:

	def reverseStr(self, s, k):
		res = ''
		i = 0
		while i < len(s):
			if i + k <= len(s) and i + 2 * k > len(s) or i + k > len(s):
				sub = s[i:i + k]
				res += sub[::-1]
				if i + 2 * k > len(s):
					res += s[i + k:]
				i = len(s)
			else:
				sub = s[i:i + k]
				res += sub[::-1]
				res += s[i + k:i + 2 * k]
				i += 2 * k
		return res


class Solution:

	def reverseStr(self, s, k):
		list_s = list(s)
		n = len(list_s)
		for i in range(0, n, 2 * k):
			if n - i >= k:
				list_s[i:i + k] = reversed(list_s[i:i + k])
			elif n - i < k:
				list_s[i:] = reversed(list_s[i:])
		return ''.join(list_s)


class Solution:

	def reverseStr(self, s, k):
		n = len(s)
		p = 0
		s = list(s)
		while True:
			if p >= n:
				break
			p1 = p + k
			if p1 > n:
				p1 = n
			p0 = p
			while p0 < p1:
				p1 -= 1
				(s[p0], s[p1]) = (s[p1], s[p0])
				p0 += 1
			p += k * 2
		return ''.join(s)


class Solution:

	def reverseStr(self, s, k):
		rst = ''
		for i in range(len(s)):
			if i // k % 2:
				rst += s[i]
			else:
				rst += s[min(len(s), (i // k + 1) * k) - i % k - 1]
		return rst


class Solution:

	def reverseStr(self, s, k):
		output_chars = list()
		num_segments = len(s) // k + 1
		for j in range(num_segments):
			start = 2 * j * k
			end = 2 * (j + 1) * k
			reverse_up_to = min(start + k, len(s))
			seg = min(start + 2 * k, len(s))
			for i in range(reverse_up_to - 1, start - 1, -1):
				output_chars.append(s[i])
			for i in range(reverse_up_to, seg):
				output_chars.append(s[i])
		return ''.join(output_chars)


class Solution:

	def reverseStr(self, s, k):
		for i in range(0, len(s), 2 * k):
			s1 = s[0:i]
			s2 = s[i:i + k][::-1]
			s3 = s[i + k:]
			s = s1 + s2 + s3
		return s


class Solution:

	def reverseStr(self, s, k):
		result = ''
		for i in range(0, len(s), 2 * k):
			result += s[i:i + k][::-1]
			result += s[i + k:i + 2 * k]
		return result


class Solution:

	def reverseStr(self, s, k):
		r = []
		k_str = ''
		for i in range(len(s)):
			if i % k == 0:
				if i != 0:
					r.append(k_str)
				k_str = s[i]
			else:
				k_str += s[i]
		r.append(k_str)
		for i in range(len(r)):
			if i % 2 == 0:
				r[i] = r[i][::-1]
		return ''.join(r)


class Solution:

	def reverseStr(self, s, k):
		l = []
		i = 0
		c = 0
		strlen = len(s)
		while i < strlen:
			if c % 2:
				l.append(s[i:i + k])
			else:
				temp = s[i:i + k]
				l.append(temp[::-1])
			c = c + 1
			i = i + k
		return ''.join(l)


class Solution:

	def reverseStr(self, s, k):
		w = []
		idx = 0
		while idx < len(s):
			w.append(s[idx:idx + k])
			idx += k
		w = [w[i][::-1] if i % 2 == 0 else w[i] for i in range(len(w))]
		return ''.join(w)


class Solution:

	def reverseStr(self, s, k):
		s = list(s)
		for i in range(0, len(s), 2 * k):
			s[i:i + k] = reversed(s[i:i + k])
		s = ''.join(s)
		return s


class Solution:

	def reverseStr(self, s, k):
		ls = [c for c in s]
		for i in range(0, len(s), 2 * k):
			start = i
			end = min(i + k, len(s)) - 1
			if end >= len(s):
				break
			while end > start:
				swp = ls[start]
				ls[start] = ls[end]
				ls[end] = swp
				start += 1
				end -= 1
		return ''.join(ls)


class Solution:

	def reverseStr(self, s, k):
		s = list(s)
		for start in range(0, len(s), 2 * k):
			p1 = start
			p2 = p1 + k - 1
			if p2 >= len(s):
				p2 = len(s) - 1
			while p1 < p2:
				(s[p1], s[p2]) = (s[p2], s[p1])
				p1 += 1
				p2 -= 1
		result = ''
		for ele in s:
			result += ele
		return result


class Solution:

	def reverseStr(self, s, k):
		i = 0
		tmp = 0
		ret = ''
		while i < len(s):
			if len(s) - i < k:
				tmp = len(s) - i
			else:
				tmp = k
			ttmp = tmp
			while ttmp != 0:
				ret += s[i + ttmp - 1]
				ttmp -= 1
			i += tmp
			if len(s) - i < k:
				tmp = len(s) - i
			else:
				tmp = k
			ttmp = tmp
			while ttmp != 0:
				ret += s[i]
				i += 1
				ttmp -= 1
		return ret


class Solution:

	def reverseStr(self, s, k):
		ans = ''
		i = 0
		while i < len(s):
			rev = ''
			j = 0
			while i + j < len(s) and j < k:
				rev += s[i + j]
				j += 1
			i += j
			ans += rev[::-1]
			j = 0
			while i + j < len(s) and j < k:
				ans += s[i + j]
				j += 1
			i += j
		return ans