Check if count of Alphabets and count of Numbers are equal in the given String
Last Updated :
06 Dec, 2022
Given the alphanumeric string str, the task is to check if the count of Alphabets and count of Numbers are equal or not.
Examples:
Input: str = “GeeKs01234”
Output: Yes
Explanation:
The count of alphabets and numbers are equal to 5.
Input: str = “Gfg01234”
Output: No
Explanation:
The count of the alphabet is 3, whereas the count of numbers is 5.
Approach: The idea is to use the ASCII values of the characters to distinguish between a number and an alphabet. After distinguishing the characters, two counters are maintained to count the number of alphabets and numbers respectively. Finally, both the counters are checked for equality.
The ASCII ranges of the characters are as follows:
- Lowercase characters: 97 to 122
- Uppercase characters: 65 to 90
- Digits: 48 to 57
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countOfLetters(string str)
{
int letter = 0;
for ( int i = 0; i < str.length(); i++) {
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
letter++;
}
return letter;
}
int countOfNumbers(string str)
{
int number = 0;
for ( int i = 0; i < str.length(); i++) {
if (str[i] >= '0' && str[i] <= '9' )
number++;
}
return number;
}
void check(string str)
{
if (countOfLetters(str)
== countOfNumbers(str))
cout << "Yes\n" ;
else
cout << "No\n" ;
}
int main()
{
string str = "GeeKs01324" ;
check(str);
return 0;
}
|
Java
public class GFG
{
static int countOfLetters(String str)
{
int letter = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z' )
|| (str.charAt(i) >= 'a' && str.charAt(i) <= 'z' ))
letter++;
}
return letter;
}
static int countOfNumbers(String str)
{
int number = 0 ;
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) >= '0' && str.charAt(i) <= '9' )
number++;
}
return number;
}
static void check(String str)
{
if (countOfLetters(str)
== countOfNumbers(str))
System.out.print( "Yes\n" );
else
System.out.print( "No\n" );
}
public static void main(String[] args)
{
String str = "GeeKs01324" ;
check(str);
}
}
|
Python3
def countOfLetters(string ) :
letter = 0 ;
for i in range ( len (string)) :
if ((string[i] > = 'A' and string[i] < = 'Z' )
or (string[i] > = 'a' and string[i] < = 'z' )) :
letter + = 1 ;
return letter;
def countOfNumbers(string ) :
number = 0 ;
for i in range ( len (string)) :
if (string[i] > = '0' and string[i] < = '9' ) :
number + = 1 ;
return number;
def check(string) :
if (countOfLetters(string) = = countOfNumbers(string)) :
print ( "Yes" );
else :
print ( "No" );
if __name__ = = "__main__" :
string = "GeeKs01324" ;
check(string);
|
C#
using System;
class GFG
{
static int countOfLetters(String str)
{
int letter = 0;
for ( int i = 0; i < str.Length; i++)
{
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
letter++;
}
return letter;
}
static int countOfNumbers(String str)
{
int number = 0;
for ( int i = 0; i < str.Length; i++)
{
if (str[i] >= '0' && str[i] <= '9' )
number++;
}
return number;
}
static void check(String str)
{
if (countOfLetters(str)
== countOfNumbers(str))
Console.Write( "Yes\n" );
else
Console.Write( "No\n" );
}
public static void Main(String[] args)
{
String str = "GeeKs01324" ;
check(str);
}
}
|
Javascript
<script>
function countOfLetters( str)
{
var letter = 0;
for ( var i = 0; i < str.length; i++) {
if ((str[i] >= 'A' && str[i] <= 'Z' )
|| (str[i] >= 'a' && str[i] <= 'z' ))
letter++;
}
return letter;
}
function countOfNumbers( str)
{
var number = 0;
for ( var i = 0; i < str.length; i++) {
if (str[i] >= '0' && str[i] <= '9' )
number++;
}
return number;
}
function check( str)
{
if (countOfLetters(str) == countOfNumbers(str))
document.write( "Yes<br>" );
else
document.write( "No<br>" );
}
var str = "GeeKs01324" ;
check(str);
</script>
|
Time Complexity: O(len), where len is the length of the string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...