Friday, 31 March 2017

Amazon interview question - To find the first repeated character in a String

Question: How to find the first repeated character in a String?

The below java program has been written in easy way to make the beginners understand how easy the way of coding is and how easily a concept can be achieved through programming.

import java.util.Scanner;


public class StrRepeat {
public static void main(String args[]){
//Get the string to check the non repreated character
System.out.println("Enter the String: ");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char fin = checkLetter(str);
System.out.println("The character is: " + fin);
s.close();
}

//To check each individual char for non repeated
private static char checkLetter(String str) {
// TODO Auto-generated method stub
//To split the string to character
char[] ary = str.toCharArray();
char val = 0;
//To get the length of character
int len = ary.length;
char temp[] = new char[len];
for (int i=0; i<len; i++){
for (int j=0; j<len; j++){
if (j!= i){
if(ary[i] == ary[j]){
temp[i] = '$';
}
}
}
}
for (int k = 0; k< len; k++){
if (temp[k] == '$' ){
val = ary[k];
break;
}
}
return val;
}


}

Output:
Enter the String: lmadam
The character is: m

Amazon interview question - Find first non repeated character in a string

Question: how to find the first non-repeated character in a String?

The below Java program has been optimised and written easy for understanding.

import java.util.Scanner;


public class StrRepeat {
           public static void main(String args[]){
          //Get the string to check the non repreated character
          System.out.println("Enter the String: ");
          Scanner s = new Scanner(System.in);
          String str = s.nextLine();
          char fin = checkLetter(str);
          System.out.println("The character is: " + fin);
          s.close();
}

//To check each individual char for non repeated
private static char checkLetter(String str) {
// TODO Auto-generated method stub
           //To split the string to character
           char[] ary = str.toCharArray(); 
           char val = 0;
           //To get the length of character
           int len = ary.length;
           char temp[] = new char[len];
           for (int i=0; i<len; i++){
                 for (int j=0; j<len; j++){
                    if (j!= i){
                         if(ary[i] == ary[j]){
                                   temp[i] = '$';
                         }
                   }
            }
}
for (int k = 0; k< len; k++){
if (temp[k] != '$' ){
val = ary[k];
break;
}
}
return val;
}


}

Output:
Enter the String: madam
The character is: d

Simple Java program for Palindrome

This program is simplified for easy understanding using the StringBuilder with reverse function in java.

import java.util.Scanner;


public class Palindrome {
public static void main(String args[]){
//Get the string to check 
System.out.println("Enter the string: ");
Scanner sc = new Scanner(System.in);
String inp = sc.nextLine();
sc.close();
//String to string builder to store in heap
StringBuilder sb = new StringBuilder();
sb.append(inp);
//Reverse function over the string 
sb = sb.reverse();
String temp = sb.toString();
//compare to check Palindrome
if (inp.equals(temp)){
System.out.println("The given string is a Palindrome.");
}else{
System.out.println("The given string is not a Palindrome.");
}
}


}

Output:
Enter the string: madam
The given string is a Palindrome.
Enter the string: Rajesh
The given string is not a Palindrome.

Expense Handler Application with advance technologies

Budget Planner  One of the application developed with Ionic 4 and Python-Flask.  Ionic 4 code:  https://github.com/logeshbuiltin/Expense...