HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Puzzles / Puzzle – Check if string is complete (contains all alphabets)

Puzzle – Check if string is complete (contains all alphabets)

A string is said to be complete if it contains all the characters from a to z. Given a string, check if it complete or not. e.g.

Sample Input

3
wyyga
qwertyuioplkjhgfdsazxcvbnm
ejuxggfsts

Sample Output

NO
YES
NO

Solution using for loop and indexOf()

I wrote a quick function which is able to find this complete string.

package com.howtodoinjava.examples;
 
public class CheckAllAlphabetsAlgorithms
{
    public static void main(String[] args)
    {
        System.out.println( checkAllChars( "qwertyuioplkjhgfdsAzxcvbnm" ) );
        System.out.println( checkAllChars( "123" ) );
        System.out.println( checkAllChars( "ejuxggfsts" ) );
        System.out.println( checkAllChars( "wyyga" ) );
    }
     
    private static String checkAllChars ( String input )
    {
        //If input length is less than 26 then it can never be complete
        if(input.length() < 26)
        {
            return "FALSE";
        }
                 
        for (char ch = 'A'; ch <= 'Z'; ch++)
        {
            if (input.indexOf(ch) < 0 && input.indexOf((char) (ch + 32)) < 0)
            {
                return "FALSE";
            }
        }
        return "TRUE";
    }
}

Output:

TRUE
FALSE
FALSE
FALSE

Solution using for regular expressions

Here is a (ugly because I don’t like long regex) solution to find complete string using regex.

package com.howtodoinjava.examples;
 
public class CheckAllAlphabetsAlgorithms
{
    public static void main(String[] args)
    {
        System.out.println( checkAllCharsUsingRegex( "qwertyuioplkjhgfdsAzxcvbnm" ) );
        System.out.println( checkAllCharsUsingRegex( "123" ) );
        System.out.println( checkAllCharsUsingRegex( "ejuxggfsts" ) );
        System.out.println( checkAllCharsUsingRegex( "wyyga" ) );
    }
     
    private static String checkAllCharsUsingRegex ( String input )
    {
        //If input length is less than 26 then it can never be complete
        if(input.length() < 26)
        {
            return "FALSE";
        }
         
        String regex = "(?i)(?=.*a)(?=.*b)(?=.*c)(?=.*d)(?=.*e)(?=.*f)"
                + "(?=.*g)(?=.*h)(?=.*i)(?=.*j)(?=.*k)(?=.*l)(?=.*m)(?=.*n)"
                + "(?=.*o)(?=.*p)(?=.*q)(?=.*r)(?=.*s)(?=.*t)(?=.*u)(?=.*v)"
                + "(?=.*w)(?=.*x)(?=.*y)(?=.*z).*";
                 
        if(input.matches(regex)){
            return "TRUE";
        }
        return "FALSE";
    }
}

Output:

TRUE
FALSE
FALSE
FALSE

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Ilya

    August 19, 2018

    Your offered solutions are both O(N^2) and O(1) space. By using a HashMap, you can get O(N) performance and O(N) space

Comments are closed on this article!

Search Tutorials

Java Puzzles

  • Interview Puzzles List
  • Dead vs Unreachable Code
  • Palindrome Number
  • Detect infinite loop in LinkedList
  • [i += j] Vs [i = i + j]
  • HiLo Guessing Game
  • Find All Distinct Duplicate Elements
  • TreeMap Put Operation
  • String With Nth Longest Length
  • Good String Vs Bad String
  • Complete String
  • Reverse String
  • Calculate Factorial
  • FizzBuzz Solution
  • Find Missing Number From Series
  • Create Instance Without New Keyword

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)