Problem · String

Count Substrings With Identical Characters

Learn this problem
EasyVirtu Financial logoVirtu FinancialINTERNOA

Problem statement

Given a string s, return the number of non-empty substrings in which all characters are identical.

Substrings are counted by their positions in s, so equal substring text occurring at different positions is counted separately.

Function

countIdenticalSubstrings(s: String) → int

Examples

Example 1

s = "zzzyz"return = 8

There are four one-character substrings equal to "z", two substrings equal to "zz", one substring equal to "zzz", and one substring equal to "y". The total is 4 + 2 + 1 + 1 = 8.

Example 2

s = "k"return = 1

The only non-empty substring is "k", whose characters are identical.

Constraints

  • 1 <= s.length <= 100
  • s contains only lowercase English letters.

More Virtu Financial problems

drafts saved locally
public int countIdenticalSubstrings(String s) {
    // write your code here
}
s"zzzyz"
expected8
checking account