In this post, I am going to solve this programming puzzle using two approach. First, using Regular Expression and Second, using String functions. Given below the code, kindly go through it, if you don't understand something then leave a comment, I will be happy to answer. If you have any suggestion for me to improvise the contents, please leave a comment. If you are liking my posts, then please share it with your friends.
Steps to solve the problem during interview or online test:
1. Patiently analyse the problem for 2-3 minutes, conceptualizing the needed calculations. You may rephrase the problem statement and check with the interviewer if you have fully understood the problem, as otherwise your program will not output the expected result.
2. Use your favorite IDE (if you are giving a remote interview and using own device) for Java programming, like InteliJ, Eclipse etc. Ensure that you have Java 8 or above installed and the JAVA_HOME path is set in the environment variable.
3. Create a java class file, use appropriate visibility (private, public, protected) and follow naming best practices. For online interviews/coding challenges, if there is no specific ask to show inheritance, then no need to create any interface or abstract class to complicate the excercise.
4. Create a main method to execute the program. This is also required by most of the online coding test platforms, as they run different unit tests to measure accuracy of the program.
5. It is a good practice to seek inputs from the user using meaningful prompts to make the program interactive, rather than hard coding it inside code. The inputs can also be accepted from commandline arguments.
6. Put meaningful names for the variables, but remain careful and avoid creating unnecessary variables.
7. Write the code in a simple manner. Often the code could be written in different ways, in such a situation, follow the way you are more comfortable with. Focus on writing correct logic than spending time in ornamentation or showcasing your coding prowess.
8. Before running the program for the first time, always do a proof reading and make any needed corrections, since if the first run fails, it could create a negative impact to the evaluators mind. Often we do mistakes in anxity, so keeping calm is a necessity. In organizations and enterprizes, the real-time programming will be much complex and there could be pressing deadlines to achive shorter milestones, therefore during recruitment, they observe the behaviour of the programmer to assess whether the candidate could handle work pressure, stress and the quality of work in a seamless manner.
9. Run the test with different inputs, to prove that the logic is working fine. If you are writing the code in any of the online test platform, then don't worry, it will fire unit tests upon the code submission and highlight the failures, you could correct them before the test meet its time outs.
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Java program to count occurrence of each characters within
* a String valueBelow program accepts an input from user,
* then counts the occurrence of each character in that String
* There are two implementations, first one is using Regular
* Expression and the other one is using String functions
* @Author: Susnigha Chatterjee
*/
public class CountEachCharOccurrenceInAString {
public static void main(String args[]) {
String inputText = new Scanner(System.in).nextLine().toLowerCase();
int counter=0;
char[] inputTextCharacters = inputText.toCharArray();;
//First implementation: Using Java Regular expressions
for(int x=0;x<inputTextCharacters.length; x++ ) {
Pattern objPattern = Pattern.
compile(""+inputTextCharacters[x]+"");
Matcher objMatcher = objPattern.
matcher(inputText);
counter=0;
while(objMatcher.find())
{
counter++;
}
System.out.print(inputTextCharacters[x] +" => ");
System.out.println(counter);
}
System.out.println("---------Separating the outputs----------");
//Second implementation: Using Java String functions
for (int i = 0; i < inputText.length();i++) {
counter = 0;
System.out.print(inputTextCharacters[i] +" => ");
for( int j=0; j<inputText.length(); j++) {
if((inputText.charAt(j)==inputTextCharacters[i]))
counter++;
}
System.out.println(counter);
}
}
}
Given below the outputs:
ulupolupolo
u => 3
l => 3
u => 3
p => 2
o => 3
l => 3
u => 3
p => 2
o => 3
l => 3
o => 3
-------------- Separating the outputs---------------
u => 3
l => 3
u => 3
p => 2
o => 3
l => 3
u => 3
p => 2
o => 3
l => 3
o => 3