BackeasyStrings Wipro

Count Step-Down Character Pairs Solution

Problem Statement

You are given a string s consisting of lowercase English letters. Your task is to count the number of indices i (where 0 <= i < s.length - 1) such that the character at the current position is alphabetically strictly greater than the character at the next position (i.e., s[i] > s[i+1]).

Example 1
Input
s = "dcba"
Output
3

Explanation: The adjacent pairs are ('d', 'c') at index 0, ('c', 'b') at index 1, and ('b', 'a') at index 2. In all three cases, the first character is alphabetically greater than the second.

Example 2
Input
s = "abccba"
Output
2

Explanation: The adjacent pairs are ('a','b'), ('b','c'), ('c','c'), ('c','b'), and ('b','a'). Only ('c', 'b') and ('b', 'a') satisfy the condition s[i] > s[i+1].

Constraints

  • 1 <= s.length <= 10^5
  • s consists only of lowercase English letters.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free