Repeatable chars in a string

I have a string I keep appending too upto certain amount of chars.

Is there some sort of way for me to check the string to see if I hit my limit of repeatable characters?

For example, assume I allow for 2 repeatable chars, this will be a valid
string Xxh03dhJUX, so I can append the last capital X to the string.

Since I allow for 2 repeatable characters in the string this would be an invalid string Xxh03dhJUXX (note the 3 capital X's).

The repeatable chars value will apply for all characters in the string and I obviously want to do the check before appending to the string. I think I would need a function that returns 0 for append 1 don't append would be the way to go.

Any ideas, suggestions or examples of code would be greatly appreciated.

Thanks in advance to all that answer.

echo "Xxh03dhJUX" "X" 2 | awk '{split($1,a,"");for (i in a){b[a]++};if (b[$2]<$3){exit 0}else{exit 1}}'
[ $(printf %s "$str" | tr -cd "$char" | wc -m) -lt $limit ]

bash/ksh93:

l=${str//[^$char]}
[[ ${#rc} -lt limit ]]