Check if there exists a permutation of given string which doesn’t contain any monotonous substring
Last Updated :
18 Apr, 2023
Given a string S of lowercase English alphabets, the task is to check if there exists an arrangement of string S such that it doesn’t contain any monotonous substring.
A monotonous substring has the following properties:
- Length of such substring is 2.
- Both the characters are consecutive, For example – “ab”, “cd”, “dc”, “zy” etc.
Examples:
Input: S = “abcd”
Output: Yes
Explanation:
String S can be rearranged into “cadb” or “bdac”
Input: string = “aab”
Output: No
Explanation:
Every arrangement of the string contains a monotonous substring.
Approach: The idea is to group the characters into two different buckets, where one bucket contains the characters which are at the even places and another bucket contains the characters which are at the odd places. Finally, checking for the concatenation point of both groups is not a monotonous substring.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string s)
{
bool ok = true ;
for ( int i = 0; i + 1 < s.size(); ++i)
ok &= ( abs (s[i] - s[i + 1]) != 1);
return ok;
}
string monotonousString(string s)
{
string odd = "" , even = "" ;
for ( int i = 0; i < s.size(); ++i) {
if (s[i] % 2 == 0)
odd += s[i];
else
even += s[i];
}
sort(odd.begin(), odd.end());
sort(even.begin(), even.end());
if (check(odd + even))
return "Yes" ;
else if (check(even + odd))
return "Yes" ;
return "No" ;
}
int main()
{
string str = "abcd" ;
string ans;
ans = monotonousString(str);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static boolean check(String s)
{
boolean ok = true ;
for ( int i = 0 ; i + 1 < s.length(); ++i)
ok &= (Math.abs(s.charAt(i) -
s.charAt(i + 1 )) != 1 );
return ok;
}
static String monotonousString(String s)
{
String odd = "" , even = "" ;
for ( int i = 0 ; i < s.length(); ++i)
{
char c=s.charAt(i);
if (c % 2 == 0 )
odd += c;
else
even += c;
}
char oddArray[] = odd.toCharArray();
Arrays.sort(oddArray);
odd = new String(oddArray);
char evenArray[] = even.toCharArray();
Arrays.sort(evenArray);
even = new String(evenArray);
if (check(odd + even))
return "Yes" ;
else if (check(even + odd))
return "Yes" ;
return "No" ;
}
public static void main(String []args)
{
String str = "abcd" ;
String ans;
ans = monotonousString(str);
System.out.println( ans);
}
}
|
Python3
def check(s):
ok = True
for i in range ( 0 , len (s) - 1 , 1 ):
ok = (ok & ( abs ( ord (s[i]) -
ord (s[i + 1 ])) ! = 1 ))
return ok
def monotonousString(s):
odd = ""
even = ""
for i in range ( len (s)):
if ( ord (s[i]) % 2 = = 0 ):
odd + = s[i]
else :
even + = s[i]
odd = list (odd)
odd.sort(reverse = False )
odd = str (odd)
even = list (even)
even.sort(reverse = False )
even = str (even)
if (check(odd + even)):
return "Yes"
elif (check(even + odd)):
return "Yes"
return "No"
if __name__ = = '__main__' :
str1 = "abcd"
ans = monotonousString(str1)
print (ans)
|
C#
using System;
class GFG{
static bool check( string s)
{
bool ok = true ;
for ( int i = 0; i + 1 < s.Length; ++i)
ok &= (Math.Abs(s[i] -
s[i + 1]) != 1);
return ok;
}
static string monotonousString( string s)
{
string odd = "" , even = "" ;
for ( int i = 0; i < s.Length; ++i)
{
if (s[i] % 2 == 0)
odd += s[i];
else
even += s[i];
}
char []oddArray = odd.ToCharArray();
Array.Sort(oddArray);
odd = new String(oddArray);
char []evenArray = even.ToCharArray();
Array.Sort(evenArray);
even = new String(evenArray);
if (check(odd + even))
return "Yes" ;
else if (check(even + odd))
return "Yes" ;
return "No" ;
}
public static void Main( string []args)
{
string str = "abcd" ;
string ans;
ans = monotonousString(str);
Console.Write(ans);
}
}
|
Javascript
<script>
function check(s)
{
var ok = true ;
for ( var i = 0; i + 1 < s.length; ++i)
ok &= (Math.abs(s[i] - s[i + 1]) != 1);
return ok;
}
function monotonousString(s)
{
var odd = "" , even = "" ;
for ( var i = 0; i < s.length; ++i) {
if (s[i] % 2 == 0)
odd += s[i];
else
even += s[i];
}
odd = odd.split(' ').sort().join(' ');
even = even.split(' ').sort().join(' ');
// Condition to check if the
// concatenation point doesn' t
if (check(odd + even))
return "Yes" ;
else if (check(even + odd))
return "Yes" ;
return "No" ;
}
var str = "abcd" ;
var ans;
ans = monotonousString(str);
document.write( ans );
</script>
|
Time Complexity : O(|S|*log|S|) ,where |S| is size of string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...