Given a string s and a positive integer k. You need to encrypt the given string such that each substring of size k is represented by an integer, which is obtained by the product of number of vowels and number of consonants in the substring.
Examples:
Input : s = "hello", k = 2
Output : 1101
k = 2, so each substring should be of size 2,
he, consonants = 1, vowels = 1, product = 1
el, consonants = 1, vowels = 1, product = 1
ll, consonants = 1, vowels = 0, product = 0
lo, consonants = 1, vowels = 1, product = 1
So, encrypted string is 1101.
Input : s = "geeksforgeeks", k = 5
Output : 666446666
Method 1 (Brute Force): The idea is to find each substring of size k and calculate the number of vowels and consonants, and then store the product of two.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' );
}
string encryptString(string s, int n, int k)
{
int countVowels = 0;
int countConsonants = 0;
string ans = "" ;
for ( int l = 0; l <= n - k; l++) {
countVowels = 0;
countConsonants = 0;
for ( int r = l; r <= l + k - 1; r++) {
if (isVowel(s[r]) == true )
countVowels++;
else
countConsonants++;
}
ans += to_string(countVowels * countConsonants);
}
return ans;
}
int main()
{
string s = "hello" ;
int n = s.length();
int k = 2;
cout << encryptString(s, n, k) << endl;
return 0;
}
|
Java
class GFG {
static boolean isVowel( char c) {
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
static String encryptString(String s, int n, int k) {
int countVowels = 0 ;
int countConsonants = 0 ;
String ans = "" ;
for ( int l = 0 ; l <= n - k; l++) {
countVowels = 0 ;
countConsonants = 0 ;
for ( int r = l; r <= l + k - 1 ; r++) {
if (isVowel(s.charAt(r)) == true ) {
countVowels++;
} else {
countConsonants++;
}
}
ans += String.valueOf(countVowels * countConsonants);
}
return ans;
}
static public void main(String[] args) {
String s = "hello" ;
int n = s.length();
int k = 2 ;
System.out.println(encryptString(s, n, k));
}
}
|
Python3
def isVowel(c):
return (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or
c = = 'u' )
def encryptString(s, n, k):
countVowels = 0
countConsonants = 0
ans = ""
for l in range (n - k + 1 ):
countVowels = 0
countConsonants = 0
for r in range (l, l + k):
if (isVowel(s[r]) = = True ):
countVowels + = 1
else :
countConsonants + = 1
ans + = ( str )(countVowels *
countConsonants)
return ans
if __name__ = = '__main__' :
s = "hello"
n = len (s)
k = 2
print (encryptString(s, n, k))
|
C#
using System;
public class GFG {
static bool isVowel( char c) {
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
static String encryptString(String s, int n, int k) {
int countVowels = 0;
int countConsonants = 0;
String ans = "" ;
for ( int l = 0; l <= n - k; l++) {
countVowels = 0;
countConsonants = 0;
for ( int r = l; r <= l + k - 1; r++) {
if (isVowel(s[r]) == true ) {
countVowels++;
} else {
countConsonants++;
}
}
ans += Convert.ToString(countVowels * countConsonants);
}
return ans;
}
static public void Main() {
String s = "hello" ;
int n = s.Length;
int k = 2;
Console.Write(encryptString(s, n, k));
}
}
|
PHP
<?php
function isVowel( $c )
{
return ( $c == 'a' || $c == 'e' || $c == 'i' ||
$c == 'o' || $c == 'u' );
}
function encryptString( $s , $n , $k )
{
$countVowels = 0;
$countConsonants = 0;
$ans = "" ;
for ( $l = 0; $l <= $n - $k ; $l ++)
{
$countVowels = 0;
$countConsonants = 0;
for ( $r = $l ; $r <= $l + $k - 1; $r ++)
{
if (isVowel( $s [ $r ]) == true)
$countVowels ++;
else
$countConsonants ++;
}
$ans = $ans . (string)( $countVowels *
$countConsonants );
}
return $ans ;
}
$s = "hello" ;
$n = strlen ( $s );
$k = 2;
echo encryptString( $s , $n , $k ) . "\n" ;
|
Javascript
<script>
function isVowel(c)
{
return (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' );
}
function encryptString(s, n, k)
{
var countVowels = 0;
var countConsonants = 0;
var ans = "" ;
for ( var l = 0; l <= n - k; l++) {
countVowels = 0;
countConsonants = 0;
for ( var r = l; r <= l + k - 1; r++) {
if (isVowel(s[r]) == true )
countVowels++;
else
countConsonants++;
}
ans += (countVowels * countConsonants).toString();
}
return ans;
}
var s = "hello" ;
var n = s.length;
var k = 2;
document.write( encryptString(s, n, k));
</script>
|
Method 2 (Efficient Approach):
The above approach can be optimized. In above approach, we are calculating whether a character is vowel or consonant more than one times. Now, taking a closer look at it, we can see that we are calculating the count of vowels and consonants for the same k – 1 characters twice. We can calculate the count of vowels and consonants for the characters from [1….k – 1] and [2….k-1] twice.
To avoid this, we can precompute the count of vowels and consonants till each index i and later use those values to calculate the result.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' );
}
string encryptString(string s, int n, int k)
{
int cv[n], cc[n];
if (isVowel(s[0]))
cv[0] = 1;
else
cc[0] = 1;
for ( int i = 1; i < n; i++) {
cv[i] = cv[i - 1] + isVowel(s[i]);
cc[i] = cc[i - 1] + !isVowel(s[i]);
}
string ans = "" ;
int prod = 0;
prod = cc[k - 1] * cv[k - 1];
ans += to_string(prod);
for ( int i = k; i < s.length(); i++) {
prod = (cc[i] - cc[i - k]) * (cv[i] - cv[i - k]);
ans += to_string(prod);
}
return ans;
}
int main()
{
string s = "hello" ;
int n = s.length();
int k = 2;
cout << encryptString(s, n, k) << endl;
return 0;
}
|
Java
class GFG
{
static boolean isVowel( char c)
{
return (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' );
}
static String encryptString( char [] s, int n, int k)
{
int [] cv = new int [n];
int [] cc = new int [n];
if (isVowel(s[ 0 ]))
cv[ 0 ] = 1 ;
else
cc[ 0 ] = 1 ;
for ( int i = 1 ; i < n; i++)
{
cv[i] = cv[i - 1 ] + (isVowel(s[i]) == true ? 1 : 0 );
cc[i] = cc[i - 1 ] + (isVowel(s[i]) == true ? 0 : 1 );
}
String ans = "" ;
int prod = 0 ;
prod = cc[k - 1 ] * cv[k - 1 ];
ans += String.valueOf(prod);
for ( int i = k; i < s.length; i++)
{
prod = (cc[i] - cc[i - k]) *
(cv[i] - cv[i - k]);
ans += String.valueOf(prod);
}
return ans;
}
public static void main(String[] args)
{
String s = "hello" ;
int n = s.length();
int k = 2 ;
System.out.print(encryptString(s.toCharArray(), n, k) + "\n" );
}
}
|
Python3
def isVowel(c):
return (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or c = = 'u' )
def encryptString(s, n, k):
cv = [ 0 for i in range (n)]
cc = [ 0 for i in range (n)]
if (isVowel(s[ 0 ])):
cv[ 0 ] = 1
else :
cc[ 0 ] = 1
for i in range ( 1 ,n):
cv[i] = cv[i - 1 ] + isVowel(s[i])
cc[i] = cc[i - 1 ] + (isVowel(s[i]) = = False )
ans = ""
prod = 0
prod = cc[k - 1 ] * cv[k - 1 ]
ans + = str (prod)
for i in range (k, len (s)):
prod = ((cc[i] - cc[i - k]) *
(cv[i] - cv[i - k]))
ans + = str (prod)
return ans
if __name__ = = '__main__' :
s = "hello"
n = len (s)
k = 2
print (encryptString(s, n, k))
|
C#
using System;
class GFG
{
static bool isVowel( char c)
{
return (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' );
}
static String encryptString( char [] s,
int n, int k)
{
int [] cv = new int [n];
int [] cc = new int [n];
if (isVowel(s[0]))
cv[0] = 1;
else
cc[0] = 1;
for ( int i = 1; i < n; i++)
{
cv[i] = cv[i - 1] +
(isVowel(s[i]) == true ? 1 : 0);
cc[i] = cc[i - 1] +
(isVowel(s[i]) == true ? 0 : 1);
}
String ans = "" ;
int prod = 0;
prod = cc[k - 1] * cv[k - 1];
ans += String.Join( "" , prod);
for ( int i = k; i < s.Length; i++)
{
prod = (cc[i] - cc[i - k]) *
(cv[i] - cv[i - k]);
ans += String.Join( "" , prod);
}
return ans;
}
public static void Main(String[] args)
{
String s = "hello" ;
int n = s.Length;
int k = 2;
Console.Write(encryptString(
s.ToCharArray(), n, k) + "\n" );
}
}
|
Javascript
<script>
function isVowel(c) {
return c === "a" || c === "e" || c === "i" ||
c === "o" || c === "u" ;
}
function encryptString(s, n, k) {
var cv = new Array(n).fill(0);
var cc = new Array(n).fill(0);
if (isVowel(s[0])) cv[0] = 1;
else cc[0] = 1;
for ( var i = 1; i < n; i++) {
cv[i] = cv[i - 1] + (isVowel(s[i]) === true ? 1 : 0);
cc[i] = cc[i - 1] + (isVowel(s[i]) === true ? 0 : 1);
}
var ans = "" ;
var prod = 0;
prod = cc[k - 1] * cv[k - 1];
ans += prod;
for ( var i = k; i < s.length; i++) {
prod = (cc[i] - cc[i - k]) * (cv[i] - cv[i - k]);
ans += prod;
}
return ans;
}
var s = "hello" ;
var n = s.length;
var k = 2;
document.write(encryptString(s.split( "" ), n, k) + "<br>" );
</script>
|
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Jul, 2022
Like Article
Save Article