Count the number of vowels and consonants in a Linked List
Last Updated :
11 Feb, 2022
Given a linked list containing lowercase English alphabets, the task is to count the number of consonants and vowels present in the linked list.
Example:
Input: Linked List: a ->b->o->y -> e ->z->NULL
Output:
Vowels : 3
Consonants: 3
Input: Linked List: a -> e -> b->c->s->e->y->t->NULL
Output:
Vowels: 3
Consonants:5
Approach: To solve this problem, follow the below steps:
- Create two variables, vowel and consonant to store the number of vowels and consonants respectively. Initialize both of them with 0.
- Now, start traversing the linked list, and increment vowel by 1 if the character matches with anyone from the set of [a, e, i, o, u] else increment consonant by 1.
- Print the answer according to the above observation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
struct Node* next;
Node( char key)
{
data = key;
next = NULL;
}
};
bool isVowel( char x)
{
return (x == 'a' || x == 'e'
|| x == 'i' || x == 'o'
|| x == 'u' );
}
void count( struct Node* head)
{
int vowel = 0;
int consonant = 0;
for (Node* itr = head; itr != NULL; itr = itr->next) {
if (isVowel(itr->data)) {
vowel++;
}
else {
consonant++;
}
}
cout << "Vowel: " << vowel << endl;
cout << "Consonant: " << consonant << endl;
}
int main()
{
Node* head = new Node( 'u' );
head->next = new Node( 'h' );
head->next->next = new Node( 'd' );
head->next->next->next = new Node( 'a' );
head->next->next->next->next = new Node( 'n' );
count(head);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node {
char data;
Node next;
Node( char key)
{
data = key;
next = null ;
}
};
static boolean isVowel( char x)
{
return (x == 'a' || x == 'e'
|| x == 'i' || x == 'o'
|| x == 'u' );
}
static void count(Node head)
{
int vowel = 0 ;
int consonant = 0 ;
for (Node itr = head; itr != null ; itr = itr.next) {
if (isVowel(itr.data)) {
vowel++;
}
else {
consonant++;
}
}
System.out.print( "Vowel: " + vowel + "\n" );
System.out.print( "Consonant: " + consonant + "\n" );
}
public static void main(String[] args)
{
Node head = new Node( 'u' );
head.next = new Node( 'h' );
head.next.next = new Node( 'd' );
head.next.next.next = new Node( 'a' );
head.next.next.next.next = new Node( 'n' );
count(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self . next = None ;
def isVowel(x):
return (x = = 'a' or x = = 'e' or x = = 'i' or x = = 'o' or x = = 'u' );
def count(head):
vowel = 0 ;
consonant = 0 ;
while (head! = None ):
if (isVowel(head.data)):
vowel + = 1 ;
else :
consonant + = 1 ;
head = head. next ;
print ( "Vowel: " , vowel , "");
print ( "Consonant: " , consonant , "");
if __name__ = = '__main__' :
head = Node( 'u' );
head. next = Node( 'h' );
head. next . next = Node( 'd' );
head. next . next . next = Node( 'a' );
head. next . next . next . next = Node( 'n' );
count(head);
|
C#
using System;
public class GFG
{
class Node {
public char data;
public Node next;
public Node( char key)
{
data = key;
next = null ;
}
};
static bool isVowel( char x)
{
return (x == 'a' || x == 'e'
|| x == 'i' || x == 'o'
|| x == 'u' );
}
static void count(Node head)
{
int vowel = 0;
int consonant = 0;
for (Node itr = head; itr != null ; itr = itr.next) {
if (isVowel(itr.data)) {
vowel++;
}
else {
consonant++;
}
}
Console.Write( "Vowel: " + vowel + "\n" );
Console.Write( "Consonant: " + consonant + "\n" );
}
public static void Main(String[] args)
{
Node head = new Node( 'u' );
head.next = new Node( 'h' );
head.next.next = new Node( 'd' );
head.next.next.next = new Node( 'a' );
head.next.next.next.next = new Node( 'n' );
count(head);
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .data = key;
this .next = null ;
}
};
function isVowel(x)
{
return (x == 'a' || x == 'e'
|| x == 'i' || x == 'o'
|| x == 'u' );
}
function count(head) {
let vowel = 0;
let consonant = 0;
for (let itr = head; itr != null ; itr = itr.next) {
if (isVowel(itr.data)) {
vowel++;
}
else {
consonant++;
}
}
document.write( "Vowel: " + vowel + "<br>" );
document.write( "Consonant: " + consonant + "<br>" );
}
let head = new Node( 'u' );
head.next = new Node( 'h' );
head.next.next = new Node( 'd' );
head.next.next.next = new Node( 'a' );
head.next.next.next.next = new Node( 'n' );
count(head);
</script>
|
Output
Vowel: 2
Consonant: 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...