FastPrepChanging Username

Changing Username

TikTok logoTikTok● MediumOA
Learn

Problem statement

A TikTok user has a username represented by a string of lowercase English letters, username. The user wants to transform their username to explore possible variations. The user can perform the following operations on the username multiple times, in any order:

  • Replace an occurrence of 'cc' with 'a'.
  • Replace an occurrence of 'dd' with 'b'.

The user may also choose not to apply any operations, leaving the username unchanged.

Given the string username, determine the total number of distinct usernames that can be generated by applying these transformations. Since the result can be large, return the total count modulo 109+7.

Function

distinctUsernames(username: String) → int

Examples

Example 1

username = "ccc"return = 3

The possible usernames that can be generated are:

  • "ccc" → "ac"
  • "ccc" → "ca"
  • "ccc" (No operation is applied)

Hence the answer is 3.

More TikTok problems

See TikTok hiring insights
public int distinctUsernames(String username) {
  // write your code here
}
username"ccc"
expected3
Checking account…