Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9306319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:08:45+00:00 2026-06-19T00:08:45+00:00

I am working on a solution to a very common Java problem involving Strings.

  • 0

I am working on a solution to a very common Java problem involving Strings. I would like to write a method to see if a string is a permutation (anagram) of another.

My solution is this: I am writing a method that takes two Strings. I iterate through one String and put the characters that are found into a HashMap, where the key is the letter, and the value is the number of occurrences of that letter, as an int. Once I finish iterating through the first String, I iterate through the second String and decrement the values of each character found in the same HashMap.

Once a key reaches zero, I remove the key/value pair altogether from the HashMap. Once I finish iterating through the second String, I then merely check to see if the HashMap is empty. If it is, then the two Strings are permutations/anagrams of one another, otherwise the method returns false.

My method is as follows:

public boolean checkPermutation (String stringA, String stringB) {

        if (stringA.length() != stringB.length()) {

            return false;

        }

        else {


        HashMap alpha = new HashMap();

        for (int i = 0; i < stringA.length(); i++) {

            if (!alpha.containsKey(stringA.charAt(i))) {

                alpha.put(stringA.charAt(i), 0);
            }

            else {

                Integer value = (Integer) alpha.get(stringA.charAt(i));
                int newValue = value.intValue();
                alpha.put(stringA.charAt(i), newValue++);

            }

        }

        for (int j = 0; j < stringB.length(); j++) {

            if (alpha.containsKey(stringB.charAt(j))) {

                Integer value = (Integer) alpha.get(stringA.charAt(j));
                int newValue = value.intValue();//null pointer exception here

                if (newValue > 1) {

                    alpha.put(stringB.charAt(j), newValue--);

                }

                else {

                    alpha.remove(stringB.charAt(j));

                }

            }

        }

        if (alpha.isEmpty())
            return true;

        else
            return false;


        }
    }

My problem is that my solution is case-sensitive (i.e. If I enter “Hello”, and “oellH”, I get a null pointer exception). I would like to make this solution case-insensitive, and figure out why I am getting a Null-Pointer Exception. Can anyone see what I am doing wrong?

As a minor follow up question, is such a solution of type O(n) despite there being two for-loops?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:08:46+00:00Added an answer on June 19, 2026 at 12:08 am
    Integer value = (Integer) alpha.get(stringA.charAt(j));
    

    Try changing stringA to stringB.

    Integer value = (Integer) alpha.get(stringB.charAt(j));
    

    Because if you give different String which dont have same characters in it the value will be null and it will through NPE.

    Change both string to lowercase or uppercase.

    stringA= stringA.toLowercase();
    stringB= stringB.toLowercase();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems a very common problem. But I couldn't find any working solution. We
I want to convert a C-style string into a byte-vector. A working solution would
I'm working on a solution to a problem where users could potentially access images
I have the very common problem of creating an index for an in-disk array
Currently I have a working application but I would like some advice because I
I would like to create a very simple eclipse project (plugin/feature/product/whatever) that when executed,
I'm very new to Scala! However, I have the following working solution to Euler
I have a working solution for my problem but now I want to improve
I am having a very common problem which it seems that all the available
Probably a very simple answer, but I cannot seem to find a working solution.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.