Skip to main content

Posts

Showing posts from October, 2015

BIG Integer

Problem Statement In this problem you have to add and multiply huge numbers! These numbers are so big that you can't contain them in any ordinary data types like long integer. Use the power of Java's BigInteger class and solve this problem. Input Format There will be two lines containing two numbers,   a   and   b . The numbers are non-negative and can have maximum 200 digits. Output Format Output two lines. The first line should contain   a + b , and the second line should contain   a × b . Don't print any leading zeros. Sample Input 1234 20 Sample Output 1254 24680 Explanation 1234+20=1254 1234*20=24680 import java.io.*; import java.util.*; import java.math.*; public class Solution {     static BigDecimal add(BigDecimal a,BigDecimal b){return a.add(b);}                 static BigDecimal multiply(BigDecimal a,BigDecimal b){return a.multiply(b);          }     public static void main(String[] args) {         /* Enter your

Alternate Characters

Problem Statement Shashank likes strings in which consecutive characters are different. For example, he likes ABABA , while he doesn't like   ABAA . Given a string containing characters   A   and   B   only, he wants to change it into a string he likes. To do this, he is allowed to delete the characters in the string. Your task is to find the minimum number of required deletions. Input Format The first line contains an integer   T , i.e. the number of test cases.   The next   T   lines contain a string each. Output Format For each test case, print the minimum number of deletions required. Constraints 1≤ T ≤10   1≤   length of string   ≤10 5 Sample Input 5 AAAA BBBBB ABABABAB BABABA AAABBB Sample Output 3 4 0 0 4 Explanation AAAA   ⟹   A ,   3   deletions BBBBB   ⟹   B ,   4   deletions ABABABAB   ⟹   ABABABAB ,   0   deletions BABABA   ⟹   BABABA ,   0   deletions AAABBB   ⟹   AB ,   4   deletions because to convert it to

Validate an IP Address

Problem Statement Write a class called   myRegex   which will contain a string pattern. You need to write a regular expression and assign it to the pattern such that it can be used to validate an IP address. Use the following definition of an IP address: IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3. Some valid IP address: 000.12.12.034 121.234.12.12 23.45.12.56 Some invalid IP address: 000.12.234.23.23 666.666.23.23 .213.123.23.32 23.45.22.32. I.Am.not.an.ip In this problem you will be provided strings containing any combination of ASCII characters. You have to write a regular expression to find the valid IPs. Just write the myRegex class, and we will append your code after the following piece of code automatically before running it: import java.util.regex.Matcher; import java.util.regex.Pat