Timer is Running

String Modification
Submissions: 1848   Accuracy:

34.68%

  Difficulty: Easy   Marks: 2
Associated Course(s):   Amazon SDE Test Series

Given a string with repeated characters, the task is to complete the function rearrangeString which rearrange characters in a string so that no two adjacent characters are same, and returns 1 if it is possible to transform the string, otherwise 0.

Note : It may be assumed that the string has only lowercase English alphabets and such transformation is always always possible.

Example

Input:
str = "aaabc"
Output:
1
Explanation:
We can transform the string to abaca.

Example

Input:
aaabb
Output:
1
Explanation:
We can transform the string to ababa.

Your Task:
Complete the function rearrangeString() which takes a string as an input parameter and returns 1 if it is possible to transform the string, otherwise returns 0.

Expected Time Complexity: O(N * log N), N = length of string.
Expected Auxiliary Space: O(26)

Constraints:
1<=T<=100
1<=|s|<=100

 

Custom Input Starts
The first line of input contains an integer T denoting the no of T test cases.Then T test cases follow. The only line of every test case consists of String s.

Example:

1
aaabb

Custom Input Ends

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Raj Seth, Harshit Sidhwa
Author: saksham seth


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.