Skip to main content

Count Rotations With Exactly K Equal Adjacent Pairs - Video Solutions

Easy

Count Rotations With Exactly K Equal Adjacent Pairs | LeetCode 4043 | Weekly Contest 518 | Java

Developer Coder
17:20230 views
5 video solutions available

Count Rotations With Exactly K Equal Adjacent Pairs - Video Solution

Watch 5 video solutions for Count Rotations With Exactly K Equal Adjacent Pairs, a easy level problem. This walkthrough by Developer Coder has 230 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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:

  • "aab": The characters at positions 0 and 1 are equal, so score = 1.
  • "aba": No two adjacent characters are equal, so score = 0.
  • "baa": The characters at positions 1 and 2 are equal, so score = 1.

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:

  • "abca": No two adjacent characters are equal, so score = 0.
  • "bcaa": The characters at positions 2 and 3 are equal, so score = 1.
  • "caab": The characters at positions 1 and 2 are equal, so score = 1.
  • "aabc": The characters at positions 0 and 1 are equal, so score = 1.

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

 

Constraints:

  • 2 <= n == s.length <= 100
  • s only consists of lowercase English letters.
  • 0 <= k <= n - 1
Read full problem with examples