Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Sunday, December 13, 2020

Print name of each digit of a number in Java

 Challenge is to write a method to print the passed number using words for each of the digits. If the number is negative then print "Invalid Value". 

I simply used the String manipulations in Java to resolve this issue with aid of a Switch statement to identify each digit and get the corresponding name of the digit.

Below is my working solution and I hope it is helpful.

public class NumberToWords {
public static void numberToWords(int number){
String resultString = "";
if(number < 0){
resultString = "Invalid Value";
}else{
String numberString = String.valueOf(number);

for(int i=0; i<numberString.length(); i++){
int currentNumber = Character.getNumericValue
(numberString.charAt(i));

resultString = resultString +
getNameOfNumber(currentNumber) + " ";
}
}
System.out.println(resultString);
}

public static String getNameOfNumber(int num){
String numberName = "";
switch (num){
case 0 :
numberName = "Zero";
break;
case 1:
numberName = "One";
break;
case 2:
numberName = "Two";
break;
case 3:
numberName = "Three";
break;
case 4:
numberName = "Four";
break;
case 5:
numberName = "Five";
break;
case 6:
numberName = "Six";
break;
case 7:
numberName = "Seven";
break;
case 8:
numberName = "Eight";
break;
case 9:
numberName = "Nine";
break;
default:
numberName = "Invalid";
break;
}
return numberName;
}
}



Java program to get only the even digit sum of a given integer

 Yet another coding challenge I came across and here is the challenge ; 

you are given an Integer number

return - 1 if it is a negative number

if not, calculate the sum of all the even digits of the given number

    e.g:- 1658 is given then sum = 6 + 8 which is 14

below is my solution and I hope it helps

public class EvenDigitSum {
public static int getEvenDigitSum(int number){
int sumOfEvenDigits = 0;

if(number < 0){
sumOfEvenDigits = -1;
}else{
int lengthOfNumber = String.valueOf(number).length();

for(int i=0 ; i< lengthOfNumber ; i++){
int currentNumber = Character.getNumericValue(String.valueOf(number).charAt(i));
if(currentNumber % 2 == 0){
sumOfEvenDigits += currentNumber;
}else{
continue;
}
}
}
return sumOfEvenDigits;
}
}


How to check a given number is a palindrome in Java ?

This is another online coding challenge I came across recently to which I found a simple solution with the use of String and StringBuffer in java. 

Let me explain the challenge here,

  • A given number is a palindrome when reversed is equal to the original number (e.g : 121, 45654,1001)
  • If the given number is a palindrome, then return true else return false
In my solution I basically convert the integer number to a string in order to reverse the number, Unfortunately java doesn't have a reverse method in String class (it's java 11 that I'm working with). However, StringBuilder has a reverse method to which a String needs to be parsed. So, here's what I'm doing.

get the Integer number --> convert to String --> convert to StringBuilder --> reverse the string --> convert back to String --> convert the string back to number --> store it in a new int variable --> compare with the original number --> return true if matches or return false unless otherwise

why do I use Math.abs() ?

This is simply to handle any negative numbers. for an instance, -232 is a palindrome but if we straight away convert it to string this will reverse the string as 232- which ends up with an invalid integer. To handle this, I have considered the absolute value by using Math.abs() function to only get the absolute value of the given number.

find my solution as below

public class NumberPalindrome {
public static boolean isPalindrome(int number){
boolean result = false;
number = Math.abs(number);
int reversedNumber = Integer.parseInt(new StringBuilder(String.valueOf(number)).reverse().toString());

if(number == reversedNumber){
result = true;
}
return result;
}
}

Simple Java program to calculate the sum of all the digits of a number

 I just came across a simple yet an interesting challenge when I was attempting some online coding practices and thought to share it with you. Firstly would like to state that there can be many ways to separate each digit of the given number but I have used the most convenient way by converting it to String and processing.

Let me explain you the challenge.

  • An integer number is given and it should be at least a two digit number (15, 896 etc but not 9). Return -1 if this is not met.
  • If the above condition is met, then sum up all the digits of the number and return.
find my solution as below

public class DigitSum {
public static int sumDigits(int number){
int sum = 0;
if(number >= 10){
int numOfDigits = String.valueOf(number).length();

for(int i=0; i<numOfDigits ; i++){
sum += Character.getNumericValue(String.valueOf(number).charAt(i));
}
}else{
sum = -1;
}
return sum;
}
}

Sunday, November 29, 2020

How to check two strings have a common substring in Java?

Given two strings, determine if they share a common substring. A substring may be as small as one character.

For example, the words "j", "and", "jug" share the common substring 'j' The words "or" and "fit" do not share a substring.

Input Format

The first line contains a single integer , the number of test cases.

The following  pairs of lines are as follows:

  • The first line contains string s1
  • The second line contains string s2

Output Format

For each pair of strings, return YES or NO.

Sample Input

2

she

saw

dress

pat

Sample Output

YES

NO

There is a basic approach for this challenge which by using a nested for loop we can detect common substrings (outer for loop traverse through s1 and the inner for loop traverse through s2. For each character in s1, each character of s2 will be matched and if same character is found , return YES and break) , however this solution is not efficient because its complexity is O(n^2).

Below approach is more efficient in which I store each character of the s1 string in a Set and then traverse through s2 String to find whether the above set contains any of the character of s2. 

Find my solution below.

static String twoStrings(String s1, String s2) {
String result="NO";
Set<Character> set1 = new HashSet<Character>();

for (char s : s1.toCharArray()){
set1.add(s);
}

for(int i=0;i<s2.length();i++){
if(set1.contains(s2.charAt(i))){
result = "YES";
break;
}
}
return result;
}