The reason a security deposit is required by most landlords is exactly for the reason you stated in your question. Now if the carpet was not cleaned before the tenant moved in, the tenant should have made an inquiry to the landlord. for example before the lease is signed the tenant and landlord should have had an agreement regarding the carpet in the lease. If there was no lease, the tenant has every right to ask the landlord to have the carpet cleaned before they move in. In other words...'let the buyer beware'. The argument of a 'dirty carpet when I moved in' has no legal bearing in court.
First, you want to review your lease and see if it addresses the issue of paying for carpet cleaning. Second, some states have laws that require the landlord provide you with a check-in list where you list any problems with your lease space when you move in. This way you have identified the problem with the apartment, so you won't be charged. Lastly, states are not uniform how they handle this question, but in most this is a maintenance item that should be handled by the landlord. A local attorney in your city could provide you with a quick and definite answer to the question.