Daily Archives: November 15, 2016

CamelCase

Alice wrote a sequence of words in CamelCase as a string of letters,s , having the following properties:

1. It is a concatenation of one or more words consisting of English letters.
2. All letters in the first word are lowercase.
3. For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, print the number of words in s on a new line.

Input Format:

A single line containing string s.

Constraints:
1<=|s|<=10^5

Output Format:

Print the number of words in string s.

Sample Input:

saveChangesInTheEditor

Sample Output

5

Explanation:

String s contains five words:

1. save
2. Changes
3. In
4. The
5. Editor

Thus, we print 5 on a new line.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String[]arr=s.split("[A-Z]");
System.out.println(arr.length);

}
}

Insertion Sort(hackerrank problem)

Sorting
One common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms.

Insertion Sort
These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with an already sorted list.

Insert element into sorted list
Given a sorted list with an unsorted number in the rightmost cell, can you write some simple code to insert into the array so that it remains sorted?

Print the array every time a value is shifted in the array until the array is fully sorted. The goal of this challenge is to follow the correct order of insertion sort.

Guideline: You can copy the value of to a variable and consider its cell “empty”. Since this leaves an extra cell empty on the right, you can shift everything over until can be inserted. This will create a duplicate of each value, but when you reach the right spot, you can replace it with .

Input Format
There will be two lines of input:

size – the size of the array
arr – the unsorted array of integers

Output Format
On each line, output the entire array every time an item is shifted in it.

Constraints

Sample Input

5
2 4 6 8 3

Sample Output

2 4 6 8 8
2 4 6 6 8
2 4 4 6 8
2 3 4 6 8

Task

Complete the method insertionSort which takes in one parameter:

arr – an array with the value in the right-most cell.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void insertIntoSorted(int[] ar) {
// Fill up this function
int s=ar.length;
for(int i=s-1;i>=0;i–){
int v=ar[s-1];
int h=s-1;
while(h>=1&&ar[h-1]>v){
ar[h]=ar[h-1];
h–;
printArray(ar);
}
ar[h]=v;

}
printArray(ar);
}

/* Tail starts here */
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int[] ar = new int[s];
for(int i=0;i<s;i++){
ar[i]=in.nextInt();
}
insertIntoSorted(ar);
}

private static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
System.out.println("");
}

}

Parenthesis Checker(java implementation)

Given an expression string exp, examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.
For example, the program should print ‘balanced’ for exp = “[()]{}{[()()]()}” and ‘not balanced’ for exp = “[(])”

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class GFG {
public static boolean isBalanced(String s){
Stackstack=new Stack();
for(int i = 0; i 0){
String st=sc.next();
//boolean bol=isBalanced(st);
if(isBalanced(st)){
System.out.println(“balanced”);
}else{
System.out.println(“not balanced”);
}

}
}
}