LeetCode-in-Java

4043. Count Rotations With Exactly K Equal Adjacent Pairs

Easy

You are given a string s of length n and an integer k.

A cyclic rotation of s is obtained by choosing a prefix of s whose length is between 0 and n - 1 (inclusive), and moving it to the end of the string while preserving the order of all characters.

For every cyclic rotation of s, let its score be the number of indices i such that 0 <= i < n - 1 and the characters at positions i and i + 1 are equal.

Return the number of cyclic rotations of s whose score equals k.

Example 1:

Input: s = “aab”, k = 1

Output: 2

Explanation:

The cyclic rotations of s are:

Since score equals k for 2 cyclic rotations of s, the answer is 2.

Example 2:

Input: s = “abca”, k = 0

Output: 1

Explanation:

The cyclic rotations of s are:

Since score equals k for only 1 cyclic rotation of s, the answer is 1.

Constraints:

Solution

public class Solution {
    public int countRotations(String s, int k) {
        int n = s.length();
        int same = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == s.charAt((i + 1) % n)) {
                same++;
            }
        }
        if (k == same - 1) {
            return same;
        } else {
            return k == same ? n - same : 0;
        }
    }
}