1. 程式人生 > 其它 >[LeetCode] 1758. Minimum Changes To Make Alternating Binary String

[LeetCode] 1758. Minimum Changes To Make Alternating Binary String

You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa.

The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not.

Return the minimum number of operations needed to make

 s alternating.

Example 1:

Input: s = "0100"
Output: 1
Explanation: If you change the last character to '1', s will be "0101", which is alternating.

Example 2:

Input: s = "10"
Output: 0
Explanation: s is already alternating.

Example 3:

Input: s = "1111"
Output: 2
Explanation: You need two operations to reach "0101" or "1010".

Constraints:

  • 1 <= s.length <= 104
  • s[i] is either '0' or '1'.

生成交替二進位制字串的最少運算元。

給你一個僅由字元 '0' 和 '1' 組成的字串 s 。一步操作中,你可以將任一 '0' 變成 '1' ,或者將 '1' 變成 '0' 。

交替字串 定義為:如果字串中不存在相鄰兩個字元相等的情況,那麼該字串就是交替字串。例如,字串 "010" 是交替字串,而字串 "0100" 不是。

返回使 s 變成 交替字串 所需的 最少 運算元。

來源:力扣(LeetCode)
連結:https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這道題算是字串型別的題目。我自己做的時候一開始沒有想到太簡潔的方法,所以分別模擬了 0101 和 1010 兩種字串,然後和 input 字串比較,看看 diff1 小還是 diff2 小,小的那個就是最少運算元。

時間O(n)

空間O(n)

Java實現

 1 class Solution {
 2     public int minOperations(String s) {
 3         int len = s.length();
 4         char[] inputArray = s.toCharArray();
 5         char[] first = new char[len];
 6         boolean zero = true;
 7         for (int i = 0; i < len; i++) {
 8             if (zero) {
 9                 first[i] = '0';
10             } else {
11                 first[i] = '1';
12             }
13             zero = !zero;
14         }
15         
16         
17         boolean one = true;
18         char[] second = new char[len];
19         for (int i = 0; i < len; i++) {
20             if (one) {
21                 second[i] = '1';
22             } else {
23                 second[i] = '0';
24             }
25             one = !one;
26         }
27         
28         int count1 = 0, count2 = 0;
29         for (int i = 0; i < len; i++) {
30             if (inputArray[i] != first[i]) {
31                 count1++;
32             }
33             if (inputArray[i] != second[i]) {
34                 count2++;
35             }
36         }
37         return Math.min(count1, count2);
38     }
39 }

後來看了評論區的這個帖子,感覺最近還是題刷少了。他的思路是,最理想情況下,字串應該是類似 01010101 這樣的排列。在這種排列中,每個位置上的字串與其下標 index 有如下關係

index  = 0, 1, 2, 3, 4 % 2

char   = 0, 1, 0, 1, 0

所以我們可以遍歷一遍 input 字串,看一下每個位置的下標 index % 2 之後是否等於同位置上的字串,記錄全域性不同的個數 diff。注意因為字串可以是 0101 型別,也可以是 1010 型別的,所以最後的結果是 Math.min(diff, len - diff)。

時間O(n)

空間O(1)

Java實現

 1 class Solution {
 2     public int minOperations(String s) {
 3         int len = s.length();
 4         int diff = 0;
 5         for (int i = 0; i < s.length(); i++) {
 6             if (i % 2 != s.charAt(i) - '0') {
 7                 diff++;
 8             }
 9         }
10         return Math.min(diff, len - diff);
11     }
12 }

LeetCode 題目總結