Psalm 51:1-4.
Have mercy upon me, O God, according to thy lovingkindness: according unto the multitude of Thy tender mercies blot out my transgressions.
Wash me throughly from mine iniquity, and cleanse me from my sin.
For I acknowledge my transgressions: and my sin is ever before me.
Against Thee, Thee only, have I sinned, and done this evil in Thy sight: that Thou mightest be justified when Thou speakest, and be clear when Thou judgest.

The story behind this psalm is one of lust, deceit, and murder–all on the part of the man who is called “a man after God’s own heart.”
David was first designated as such when Samuel anointed him to be king after Saul (I Sam. 13:14). David was quite literally just a boy at that time, and had not started upon his path of grief and glory. We find this description of David again in Acts 13:22. If you’re wondering how it could be that David is called such, I found several interesting articles. Here’s the link to the one I liked the best: https://www.biblestudytools.com/bible-study/explore-the-bible/10-reasons-david-is-called-a-man-after-god-s-own-heart.ht
David was a sinner. He knew it. He didn’t deny it when Nathan, God’s prophet, confronted him with his great sin. What a dramatic moment that was! You can read about it in II Samuel 12. Nathan had the courage of a lion, to confront David. David could have had him killed on the spot, but I believe God was already working in David’s heart so soften him and prepare him to accept God’s judgment on his sin.
For me, the most amazing thing in that entire story is David’s heartfelt, soul-deep contrition as he knelt before God i n humility and acknowledged his sin with no excuses, no “yeahbut” nonsense. In fact, I think it was a great relief to him to finally bow before God and confess freely and without reservation the evil of which he was guilty.
Once sin is acknowledged and confessed, then healing and restoration can begin.
David acknowledged his sin. At the same time, he acknowledged God’s right and righteousness in declaring the penalty for his sin. He said, in effect, “God, You are righteous and holy. Whatever You choose for me, because of my sin, is righteous and holy. I accept my guilt, and I accept Your judgment; I beg Your mercy and grace, although I deserve neither.”