Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
Check if K palindromic strings can be formed from a given string
Next article icon

Check if a String can be converted to Pangram in K changes

Last Updated : 01 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a String str containing only lowercase English alphabets and an integer K. The task is to check that whether the string can be converted to a Pangram by performing at most K changes. In one change we can remove any existing character and add a new character.

Pangram: A pangram is a sentence containing every letter in the English Alphabet.

Note: Given that length of string is greater than 26 always and in one operation we have to remove an existing element to add a new element.

Examples: 

Input : str = "qwqqwqeqqwdsdadsdasadsfsdsdsdasasas"         K = 4 Output : False Explanation : Making just 4 modifications in this string,  it can't be changed to a pangram.   Input : str = "qwqqwqeqqwdsdadsdasadsfsdsdsdasasas"         K = 24 Output : True Explanation : By making 19 modifications in the string,  it can be changed to a pangram.
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
 

Approach: 

  1. Traverse the string character by character to keep track of all the characters present in the array using a boolean visit array.
  2. Using a variable count, traverse the visit array to keep count of the missing characters.
  3. If count value is less than or equal to K, print True.
  4. Else print False.

Below is the implementation of above approach:

C++




// C++ program to check if a
// String can be converted
// to Pangram by atmost k modifications
#include<bits/stdc++.h>
using namespace std;
 
// Function to find if string
// can be converted to Pangram
// by atmost k modifications
bool isPangram(string S, int k)
{
    if (S.length() < 26)
        return false;
 
    // visit array to keep track
    // of all the characters
    // present in the array
    int visited[26];
 
    for(int i = 0; i < S.length(); i++)
        visited[S[i] - 'a'] = true;
 
    // A variable to keep count
    // of characters missing
    // in the string
    int count = 0;
 
    for(int i = 0; i < 26; i++)
    {
        if (!visited[i])
            count += 1;
    }
 
    // Comparison of count
    // with given value K
    if(count <= k )
        return true;
    return false;
}
         
// Driver Code
int main()
{
 
    string S = "thequickquickfoxmumpsoverthelazydog";
    int k = 15;
     
    // function calling
    isPangram(S, k) ? cout<< "true" :
                      cout<< "false";
 
    return 0;
}
 
// This code is contributed by ChitraNayal
 
 

Java




// Java Program to check if a String can be
// converted to Pangram by atmost k modifications
 
public class GFG {
 
    // Function to find if string can be converted
    // to Pangram by atmost k modifications
    static boolean isPangram(String S, int k)
    {
        if (S.length() < 26)
            return false;
 
        // visit array to keep track of all
        // the characters present in the array
        boolean[] visited = new boolean[26];
 
        for (int i = 0; i < S.length(); i++) {
            visited[S.charAt(i) - 'a'] = true;
        }
 
        // A variable to keep count of
        // characters missing in the string
        int count = 0;
 
        for (int i = 0; i < 26; i++) {
            if (!visited[i])
                count++;
        }
         
        // Comparison of count with given value K
        if (count <= k)
            return true;
        return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        String S = "thequickquickfoxmumpsoverthelazydog";
         
        int k = 15;
         
        System.out.print(isPangram(S, k));
    }
}
 
 

Python 3




# Python 3 program to check
# if a String can be converted
# to Pangram by atmost k modifications
 
# Function to find if string
# can be converted to Pangram
# by atmost k modifications
def isPangram(S, k) :
 
    if len(S) < 26 :
        return False
 
    # visit array to keep track
    # of all the characters
    # present in the array
    visited = [0] * 26
 
    for char in S :
        visited[ord(char) - ord('a')] = True
 
    # A variable to keep count
    # of characters missing
    # in the string
    count = 0
 
    for i in range(26) :
 
        if visited[i] != True :
            count += 1
 
    # Comparison of count
    # with given value K
    if count <= k :
        return True
    return False
         
# Driver Code
if __name__ == "__main__" :
     
    S = "thequickquickfoxmumpsoverthelazydog"
    k = 15
     
    # function calling
    print(isPangram(S,k))
         
# This code is contributed by ANKITRAI1
 
 

C#




// C# Program to check if a
// String can be converted to
// Pangram by atmost k modifications
using System;
 
class GFG
{
 
// Function to find if string
// can be converted to Pangram
// by atmost k modifications
static bool isPangram(String S, int k)
{
    if (S.Length < 26)
        return false;
 
    // visit array to keep track
    // of all the characters present
    // in the array
    bool[] visited = new bool[26];
 
    for (int i = 0; i < S.Length; i++)
    {
        visited[S[i] - 'a'] = true;
    }
 
    // A variable to keep count
    // of characters missing in
    // the string
    int count = 0;
 
    for (int i = 0; i < 26; i++)
    {
        if (!visited[i])
            count++;
    }
     
    // Comparison of count with
    // given value K
    if (count <= k)
        return true;
    return false;
}
 
// Driver code
public static void Main()
{
    string S = "thequickquickfoxmumpsoverthelazydog";
     
    int k = 15;
     
    Console.WriteLine(isPangram(S, k));
}
}
 
// This code is contributed
// by inder_verma.
 
 

PHP




<?php
// PHP program to check if a
// String can be converted
// to Pangram by atmost k modifications
 
// Function to find if string
// can be converted to Pangram
// by atmost k modifications
function isPangram($S, $k)
{
    if (strlen($S) < 26)
        return false;
 
    // visit array to keep track
    // of all the characters
    // present in the array
    $visited = array_fill(0, 26, NULL);
 
    for($i = 0; $i < strlen($S); $i++)
        $visited[ord($S[$i]) -
                 ord('a')] = true;
 
    // A variable to keep count
    // of characters missing
    // in the string
    $count = 0;
 
    for($i = 0; $i < 26; $i++)
    {
        if ($visited[$i] != true)
            $count += 1;
    }
 
    // Comparison of count
    // with given value K
    if ($count <= $k )
        return true;
    return false;
}
         
// Driver Code
$S = "thequickquickfoxmumpsoverthelazydog";
$k = 15;
     
// function calling
echo isPangram($S, $k)? "true" : "false";
     
// This code is contributed by ChitraNayal
?>
 
 

Javascript




<script>
      // JavaScript Program to check if a
      // String can be converted to
      // Pangram by atmost k modifications
      // Function to find if string
      // can be converted to Pangram
      // by atmost k modifications
      function isPangram(S, k) {
        if (S.length < 26) return false;
 
        // visit array to keep track
        // of all the characters present
        // in the array
        var visited = new Array(26);
 
        for (var i = 0; i < S.length; i++) {
          visited[S[i].charCodeAt(0) - "a".charCodeAt(0)] = true;
        }
 
        // A variable to keep count
        // of characters missing in
        // the string
        var count = 0;
 
        for (var i = 0; i < 26; i++) {
          if (!visited[i]) count++;
        }
 
        // Comparison of count with
        // given value K
        if (count <= k) return true;
        return false;
      }
 
      // Driver code
      var S = "thequickquickfoxmumpsoverthelazydog";
      var k = 15;
 
      document.write(isPangram(S, k));
    </script>
 
 
Output
true

Complexity Analysis:

  • Time Complexity: O(|S|) ,where S is the given string
  • Space Complexity : O(26) ,to store characters.


Next Article
Check if K palindromic strings can be formed from a given string
author
rachana soma
Improve
Article Tags :
  • DSA
  • Strings
  • ASCII
  • cpp-strings-library
  • Hash
Practice Tags :
  • Hash
  • Strings

Similar Reads

  • Check if string S can be converted to T by incrementing characters
    Given strings S and T. The task is to check if S can be converted to T by performing at most K operations. For the ith operation, select any character in S which has not been selected before, and increment the chosen character i times (i.e., replacing it with the letter i times ahead in the alphabet
    8 min read
  • Check if one string can be converted to another
    Given two strings str and str1, the task is to check whether one string can be converted to other by using the following operation: Convert all the presence of a character by a different character. For example, if str = "abacd" and operation is to change character 'a' to 'k', then the resultant str
    8 min read
  • Check if a String can be converted to another by inserting character same as both neighbours
    Given 2 strings A and B . The task is to check if A can be converted into B by performing the following operation any number of times : Choose an index from 0 to N - 2 (suppose N is the length of A). If the character at ith index is equal to the character at (i + 1 )th index, then insert the same ch
    9 min read
  • Check if K palindromic strings can be formed from a given string
    Given a string S of size N and an integer K, the task is to find whether the characters of the string can be arranged to make K palindromic strings simultaneously. Examples: Input: S = "annabelle", K = 2 Output: Yes Explanation: All characters of string S can be distributed into "elble" and "anna" w
    7 min read
  • Check if one string can be converted to other using given operation
    Given two strings S and T of same length. The task is to determine whether or not we can build a string A(initially empty) equal to string T by performing the below operations. Delete the first character of S and add it at the front of A. Delete the first character of S and add it at the back of A.
    15+ min read
  • Check if a String contains any index with more than K active characters
    Given a string S, containing lowercase English alphabets, and an integer K, the task is to find any index of the string which consists of more than K active characters. If found, print Yes. Otherwise, print No. Count of active characters for any index is the number of characters having previous occu
    6 min read
  • Check if a String Contains only Alphabets in Java
    In Java, to check if a string contains only alphabets, we have to verify each character to make sure it falls within the range of valid alphabetic characters. There are various ways to check this in Java, depending on requirements. Example: The most common and straightforward approach to validate if
    4 min read
  • Missing characters to make a string Pangram
    Pangram is a sentence containing every letter in the English alphabet. Given a string, find all characters that are missing from the string, i.e., the characters that can make the string a Pangram. We need to print output in alphabetic order. Examples: Input : welcome to geeksforgeeksOutput : abdhij
    8 min read
  • Check if a string can be rearranged to form special palindrome
    Given a string str, the task is to check if it can be rearranged to get a special palindromic string. If we can make it print YES else print NO. A string is called special Palindrome that contains an uppercase and a lower case character of the same character in the palindrome position. Example: ABCc
    8 min read
  • Check if a string can be converted to another given string by removal of a substring
    Given two strings S and T of length N and M respectively, the task is to check if the string S can be converted to the string T by removing at most one substring of the string S. If found to be true, then print “YES”. Otherwise, print “NO”. Example: Input: S = “abcdef”, T = “abc” Output: YES Explana
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences