[Programming Question m_0006] Black and White Card

Keywords: Java

Links: https://www.nowcoder.com/questionTerminal/bfb60fce32974c45a806e567e17183ba
Source: Niuke.com

Niu Niu has n cards in a sequence. Each card is black on one side and white on the other.In the initial state, some cards are black up and some are white up.Niu Niu now wants to turn over some cards to get an alternating arrangement in which each pair of adjacent cards has a different color.Cows want to know how many cards they need to flip to make an alternate arrangement.

Enter a description:

The input consists of a string S with a length of 3 < length < 50 and only two strings,'W'and'B', representing white and black, respectively.The entire string represents the initial state of the card sequence.

Output description:

Output an integer representing the minimum number of times a cow needs to flip.

Example 1

input

BBBW

output

1

Solution ideas: search characters one by one from back to front, replace if I and I-1 are equal, and continue to search from i-1.

At this point, there are two situations: the last character flips; the last character does not flip.Compare the smaller values in both cases.

package BiShiTi;

import java.util.Scanner;

public class m_0006 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String strSeq = scan.nextLine();

		int minTimes = getMinExchangeTimes(strSeq);
		System.out.println(minTimes);
	}
	
	static int getMinExchangeTimes(String strSeq){
		int strLen = strSeq.length();
		
		// Search backwards and forwards without flipping the last one
		int mayLen1 = 0;
		String strSeq1 = strSeq;
		for (int i = strLen - 1; i >= 1; i--) {
			if (strSeq1.charAt(i) == strSeq1.charAt(i-1)) {
				mayLen1 += 1;
				if (strSeq1.charAt(i-1) == 'W') {
					String proSubStr = strSeq1.substring(0, i-1);
					String nxtSubStr = strSeq1.substring(i, strLen);
					strSeq1 = proSubStr + String.valueOf('B') + nxtSubStr;
				}else{
					String proSubStr = strSeq1.substring(0, i-1);
					String nxtSubStr = strSeq1.substring(i, strLen);
					strSeq1 = proSubStr + String.valueOf('W') + nxtSubStr;
				}
			}
		}
		
        // Search backwards and forwards, flip last
		int mayLen2 = 0;
		String strSeq2 = strSeq;
		if (strSeq2.charAt(strLen-1) == 'W') {
			String proSubStr = strSeq2.substring(0, strLen-1);
			strSeq2 = proSubStr + String.valueOf('B');
			mayLen2 += 1;
		}else{
			String proSubStr = strSeq2.substring(0, strLen-1);
			strSeq2 = proSubStr + String.valueOf('W');
			mayLen2 += 1;
		}
		for (int i = strLen - 1; i >= 1; i--) {
			if (strSeq2.charAt(i) == strSeq2.charAt(i-1)) {
				mayLen2 += 1;
				if (strSeq2.charAt(i-1) == 'W') {
					String proSubStr = strSeq2.substring(0, i-1);
					String nxtSubStr = strSeq2.substring(i, strLen);
					strSeq2 = proSubStr + String.valueOf('B') + nxtSubStr;
				}else{
					String proSubStr = strSeq2.substring(0, i-1);
					String nxtSubStr = strSeq2.substring(i, strLen);
					strSeq2 = proSubStr + String.valueOf('W') + nxtSubStr;
				}
			}
		}
		
		return mayLen1 > mayLen2 ? mayLen2:mayLen1;
	}
}

Posted by jeliot on Wed, 04 Mar 2020 08:37:48 -0800