Skip to main content

Alternating Characters [JAVA]

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.
Next T lines contain a string each.
Output Format
Print minimum number of required steps for each test case.
Constraints
1T10
1lengthofString105 
Code 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
 * @author Rakesh KR
 */
public class Solution {

 public static void main(String[] args) throws Exception{

  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  int limit = Integer.parseInt(in.readLine());
  if(limit>=1 && limit<=10){
   for(int i = 0 ; i< limit ;i++){
    StringBuffer sbStr = new StringBuffer(in.readLine());
    if(sbStr.length()>=1 && sbStr.length()<=10*10*10*10*10){
     StringBuffer delSbStr = new StringBuffer();
     for(int j=0 ; j<sbStr.length()-1 ; j++){
      if(sbStr.charAt(j)==sbStr.charAt(j+1)){
       delSbStr.append(sbStr.charAt(j));
      }
     }
     System.out.println(delSbStr.length());
     sbStr    = null;
     delSbStr = null;
    }
   }
  }
 }

}

Comments

Popular posts from this blog

Transform Ubuntu into Xubuntu

Installation 1. Open the terminal by Ctrl+Alt+T     and type    sudo apt-get install xubuntu-desktop gksu leafpad synaptic         2. Type your password & Press Enter. Now an intensive operation is being launched. Simply wait to complete the whole process. Login To Xubuntu 1. After completing the installation logout ubuntu. Note: logout not restart or shutdown. 2. In the login window click on the ubuntu logo, next to your userName & select Xubuntu Sesion 3. Enter your password and Now the Xubuntu desktop appears. :) The next thing is to clean up. Clean Up 1. Now its time to clean up, inorder to prevent system pollution problems. Note: The clean up will remove as much as possible ubuntu's desktop environment Unity. So after that you can't use Unity. 2. Open terminal by Ctrl+Alt+T and type the following  sudo apt-get remove nautilus gnome-power-manager gnome-screensaver gnome-termina*...

PostgreSQL :: open-source relational database management system (RDBMS)

 PostgreSQL also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance.  PostgreSQL features transactions with ACID properties, automatically updatable views, materialized views, triggers, foreign keys, and stored procedures. It is designed to handle a range of workloads, from single machines to data warehouses or Web services with many concurrent users. PostgreSQL manages concurrency through multiversion concurrency control (MVCC), which gives each transaction a "snapshot" of the database, allowing changes to be made without affecting other transactions.  PostgreSQL provides an asynchronous messaging system that is accessed through the NOTIFY, LISTEN and UNLISTEN commands. PostgreSQL includes built-in support for regular B-tree and hash table indexes, and four index access methods: generalized search trees (GiST), generalized inverted indexes (GIN), Space-Partitioned GiST (SP-GiST) and Bloc...

15 Free Feature Rich Bootstrap Admin Templates

1. Charisma Charisma  comes with 9 different skins/themes to suit your style and application type. It is clean, easy to use and contains over 1000 icons and 15 plugins. It is also filled with numerous UI elements like star rating, pop over, custom tooltip, alerts, Ajax loaders, notifications and much more.  2. Siminta Siminta  comes with a large collection of plugins and UI components that help you make your work easy. All codes are self-explanatory (with comments) and the overall structure is easy to customize. It is free for personal as well as commercial use. 3. Metis Metis  is a free Twitter Bootstrap template with various layouts, components, forms, tables, maps, charts and menu level. It is also equipped with file manager, maps, error page structure, grid system and login page.  4. Hierapolis Yet another private  admin template  based on Twitter Bootstrap 3. It consists of beautiful fo...