Check if a string is made up of K alternating characters
Given a string str and an integer K, the task is to check if it is made up of K alternating characters.
Examples:
Input: str = “acdeac”, K = 4
Output: Yes
Input: str = “abcdcab”, K = 2
Output: No
Approach: In order for the string to be made up of K alternating characters, it must satisfy the following conditions:
- All the characters at (i + mK) indices must be the same, where i is the current index and mK represents the mth multiple of K. It means that after every K indices, the character must get repeated.
- The adjacent characters must not be the same. This is because if the string is of type “AAAAA”, where a single character is repeated any number of time, the above condition will get matched, but the answer must be ‘No’ in this case.
Below code is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isKAlternating(string s, int k)
{
if (s.length() < k)
return false ;
int checker = 0;
for ( int i = 0; i < k; i++) {
int bitAtIndex = s[i] - 'a' ;
if ((checker & (1 << bitAtIndex)) > 0) {
return false ;
}
checker = checker | (1 << bitAtIndex);
}
for ( int i = k; i < s.length(); i++)
if (s[i - k] != s[i])
return false ;
return true ;
}
int main()
{
string str = "acdeac" ;
int K = 4;
if (isKAlternating(str, K))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
class GFG{
static boolean isKAlternating(String s, int k)
{
if (s.length() < k)
return false ;
int checker = 0 ;
for ( int i = 0 ; i < k; i++) {
int bitAtIndex = s.charAt(i) - 'a' ;
if ((checker & ( 1 << bitAtIndex)) > 0 ) {
return false ;
}
checker = checker | ( 1 << bitAtIndex);
}
for ( int i = k; i < s.length(); i++)
if (s.charAt(i - k) != s.charAt(i) )
return false ;
return true ;
}
public static void main(String[] args)
{
String str = "acdeac" ;
int K = 4 ;
if (isKAlternating(str, K))
System.out.print( "Yes" + "\n" );
else
System.out.print( "No" + "\n" );
}
}
|
Python3
def isKAlternating( s, k):
if ( len (s) < k):
return False
checker = 0
for i in range ( k):
bitAtIndex = ord (s[i]) - ord ( 'a' )
if ((checker & ( 1 << bitAtIndex)) > 0 ):
return False
checker = checker | ( 1 << bitAtIndex)
for i in range (k, len (s)):
if (s[i - k] ! = s[i]):
return False
return True
if __name__ = = "__main__" :
st = "acdeac"
K = 4
if (isKAlternating(st, K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isKAlternating(String s, int k)
{
if (s.Length < k)
return false ;
int checker = 0;
for ( int i = 0; i < k; i++) {
int bitAtIndex = s[i] - 'a' ;
if ((checker & (1 << bitAtIndex)) > 0) {
return false ;
}
checker = checker | (1 << bitAtIndex);
}
for ( int i = k; i < s.Length; i++)
if (s[i - k] != s[i] )
return false ;
return true ;
}
public static void Main()
{
String str = "acdeac" ;
int K = 4;
if (isKAlternating(str, K))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isKAlternating(s, k)
{
if (s.length < k)
return false ;
var checker = 0;
for ( var i = 0; i < k; i++) {
var bitAtIndex = s[i].charCodeAt(0) - 'a' .charCodeAt(0);
if ((checker & (1 << bitAtIndex)) > 0) {
return false ;
}
checker = checker | (1 << bitAtIndex);
}
for ( var i = k; i < s.length; i++)
if (s[i - k] != s[i])
return false ;
return true ;
}
var str = "acdeac" ;
var K = 4;
if (isKAlternating(str, K))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...