Given a string, rearrange it in such a way that it has no subsequence as cba. If there are multiple answers return the lexicographically smallest one.
Example 1:
Input:
N=6
S="aaaaab"
Output:
aaaaab
Explanation: "aaaaab" has no subsequence as cba and is 
lexicographically the smallest one.
Example 2:
Input: 
N=3 
S="cba"
Output: 
abc.
Explanation: "abc" has no subsequence as cba and is
lexicographically the smallest one.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function cbaSubsequence() which takes the string S, its size N as input parameters and returns the rearranged string.
 
Expected Time Complexity: O(NLogN)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=10^{5}
S contains only lowercase English alphabets.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Sorting

Data structures

Starter code: #User function Template for python3



class Solution():

    def cbaSubsequence(self, s, n):

        #your code goes here

Solutions:
class Solution:

	def cbaSubsequence(self, s, n):
		return ''.join(sorted(s))


class Solution:

	def cbaSubsequence(self, s, n):
		k = sorted(s)
		l = ''.join(k)
		return l


class Solution:

	def cbaSubsequence(self, s, n):
		x = sorted(s)
		c = ''
		for i in x:
			c += i
		return c


class Solution:

	def cbaSubsequence(self, s, n):
		a = sorted(s)
		st = ''
		for i in a:
			st += i
		return st


class Solution:

	def cbaSubsequence(self, s, n):
		l = []
		for i in s:
			l.append(i)
		l = sorted(l)
		a = ''
		for i in l:
			a += i
		return a


class Solution:

	def cbaSubsequence(self, s, n):
		a = list(s)
		return ''.join(sorted(a))


class Solution:

	def cbaSubsequence(self, s, n):
		if 'cba' in s:
			s = sorted(s)
			return ''.join(s)
		s = sorted(s)
		return ''.join(s)


class Solution:

	def cbaSubsequence(self, s, n):
		l = list(s)
		l.sort()
		return ''.join(l)


class Solution:

	def cbaSubsequence(self, s, n):
		for i in range(len(s)):
			if 'cba' in s:
				return ''.join(sorted(s))
		return ''.join(sorted(s))


class Solution:

	def cbaSubsequence(self, s, n):
		lis = []
		k = ''
		for i in s:
			lis.append(i)
		lis.sort()
		for i in lis:
			k += i
		return k


class Solution:

	def cbaSubsequence(self, s, n):
		lt = list(s)
		lt.sort()
		s = ''.join(lt)
		return s


class Solution:

	def cbaSubsequence(self, s, n):
		a = []
		t = ''
		b = []
		c = ''
		if 'cba' in s:
			b = sorted(s[::-1])
			for i in b:
				c += i
			return c
		else:
			a = sorted(s)
			for i in a:
				t += i
		return t


class Solution:

	def cbaSubsequence(self, s, n):
		L = [ch for ch in s]
		L.sort()
		return ''.join(L)


class Solution:

	def cbaSubsequence(self, s, n):
		lis = [i for i in s]
		lis.sort()
		return ''.join(lis)


class Solution:

	def cbaSubsequence(self, s, n):
		arr = list(s)
		arr.sort()
		a = ''
		for i in arr:
			a = a + str(i)
		return a


class Solution:

	def cbaSubsequence(self, s, n):
		arr = list(s)
		arr.sort()
		s = ''.join(arr)
		return s


class Solution:

	def cbaSubsequence(self, s, n):
		s = s.replace('cba', 'abc')
		return ''.join(sorted(s))