Java color tile Programming analysis, java Tile
Niu prefers color items, especially color tiles. Niu's room is covered with L square tiles. There are four possible colors for each brick: red, green, blue, and yellow. Given a string S, if the I-th character of S is 'R', 'G', 'B' or 'y ', the color of the first tile is red, green, blue, or yellow.
Niu decided to change the color of some tiles, so that the colors of the adjacent two tiles are different. Please help Niu calculate the minimum number of tiles to be replaced.
Input description:
The input includes one row, one string, and the string length (1 ≤ length ≤ 10). Each string in the string is 'R', 'G', 'B', or 'y '.
Output description:
Returns an integer that indicates the minimum number of tiles to be replaced.
Example 1
Input
RRRRRR
Output
3
import java.util.Scanner;public class repleaseColor { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str=sc.nextLine(); getNum(str); } private static void getNum(String str) { // TODO Auto-generated method stub char[] ch=str.toCharArray(); int tem=0; int len=ch.length; if(len>=2){ for(int i=1;i<len-1;i=i+2){ if(ch[i]==ch[i-1] || ch[i]==ch[i+1]){ tem++; } } if(ch[len-1]==ch[len-2] && len%2==0){ tem++; } for(int i=2;i<len-2;i=i+2){ if(ch[i]==ch[i-1] && ch[i]==ch[i+1] && ch[i+1]!=ch[i+2] && ch[i-1]!=ch[i-2]){ tem--; } } } System.out.println(tem); }}