Given a string of lower and uppercase characters, the task is to find how many characters are in the same position as in the English alphabet.
Examples:
Input: ABcED
Output : 3
First three characters are at same position
as in English alphabets.
Input: geeksforgeeks
Output : 1
Only 'f' is at same position as in English
alphabet
Input : alphabetical
Output : 3
For this we can have simple approach:
1) Initialize result as 0.
2) Traverse input string and do following for every
character str[i]
a) If 'i' is same as str[i] - 'a' or same as
str[i] - 'A', then do result++
3) Return result
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int findCount(string str)
{
int result = 0;
for (int i = 0 ; i < str.size(); i++)
if (i == (str[i] - 'a') || i == (str[i] - 'A'))
result++;
return result;
}
int main()
{
string str = "AbgdeF";
cout << findCount(str);
return 0;
}
Java
import java.io.*;
class GFG
{
static int findCount(String str)
{
int result = 0;
for (int i = 0; i < str.length(); i++)
{
if (i == (str.charAt(i) - 'a')
|| i == (str.charAt(i) - 'A'))
{
result++;
}
}
return result;
}
public static void main(String[] args)
{
String str = "AbgdeF";
System.out.print(findCount(str));
}
}
Python3
def findCount(str):
result = 0
for i in range(len(str)):
if ((i == ord(str[i]) - ord('a')) or
(i == ord(str[i]) - ord('A'))):
result += 1
return result
str = 'AbgdeF'
print(findCount(str))
Javascript
<script>
function findCount(str) {
var result = 0;
for (var i = 0; i < str.length; i++)
if (
i === str[i].charCodeAt(0) - "a".charCodeAt(0) ||
i === str[i].charCodeAt(0) - "A".charCodeAt(0)
)
result++;
return result;
}
var str = "AbgdeF";
document.write(findCount(str));
</script>
C#
using System;
class GFG
{
static int findCount(string str)
{
int result = 0;
for (int i = 0 ; i < str.Length; i++)
if (i == (str[i] - 'a') ||
i == (str[i] - 'A'))
result++;
return result;
}
public static void Main()
{
string str = "AbgdeF";
Console.Write(findCount(str));
}
}
PHP
<?php
function findCount($str)
{
$result = 0;
for ($i = 0; $i < strlen($str); $i++)
{
if (($i == ord($str[$i]) - ord('a')) or
($i == ord($str[$i]) - ord('A')))
$result += 1;
}
return $result;
}
$str = "AbgdeF";
print(findCount($str))
?>
Time Complexity: O(N) , where N is the length of string.Auxiliary Space: O(1).
Approach: Same Position Character Counter Initialize a counter variable to 0 to count the number of characters in the same position as in the English alphabet. Loop through each character in the input string. Convert the character to lowercase using the lower() method. Get the ASCII code of the character using the ord() method. Subtract the ASCII code of ‘a’ from the ASCII code of the character to get its position in the alphabet (0-indexed). Compare the position of the character with its index in the input string. If they are equal, increment the counter variable. Return the counter variable as the output.
C++
#include <iostream>
#include <string>
using namespace std;
int count_same_position_chars(string s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = tolower(s[i]);
if (c - 'a' == i) {
count++;
}
}
return count;
}
int main() {
string input_string = "ABcED";
int result = count_same_position_chars(input_string);
cout << result << endl;
return 0;
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String inputString = "ABcED";
int result = countSamePositionChars(inputString);
System.out.println(result);
}
public static int countSamePositionChars(String s)
{
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = Character.toLowerCase(s.charAt(i));
if (c - 'a' == i) {
count++;
}
}
return count;
}
}
Python3
def count_same_position_chars(s):
count = 0
for i, c in enumerate(s):
c = c.lower()
if ord(c) - ord('a') == i:
count += 1
return count
input_string = "ABcED"
result = count_same_position_chars(input_string)
print(result)
C#
using System;
class Program {
static void Main(string[] args) {
string inputString = "ABcED";
int result = CountSamePositionChars(inputString);
Console.WriteLine(result);
}
static int CountSamePositionChars(string s) {
int count = 0;
for (int i = 0; i < s.Length; i++) {
char c = Char.ToLower(s[i]);
if (c - 'a' == i) {
count++;
}
}
return count;
}
}
Javascript
function CountSamePositionChars(s) {
let count = 0;
for (let i = 0; i < s.length; i++) {
let c = s[i].toLowerCase();
if (c.charCodeAt(0) - 'a'.charCodeAt(0) == i) {
count++;
}
}
return count;
}
let inputString = "ABcED";
let result = CountSamePositionChars(inputString);
console.log(result);
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1), as we are only using a constant amount of extra space to store the counter variable and temporary variables.
Last Updated :
20 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...