diff --git a/dsa-problems/leetcode-problems/3000-3099.md b/dsa-problems/leetcode-problems/3000-3099.md index f109b8d3d..51a138be3 100644 --- a/dsa-problems/leetcode-problems/3000-3099.md +++ b/dsa-problems/leetcode-problems/3000-3099.md @@ -10,606 +10,606 @@ keywords: --- export const problems = [ - { - "problemName": "3000. Maximum Area of Longest Diagonal Rectangle", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle", - "solutionLink": "#" - }, - { - "problemName": "3001. Minimum Moves to Capture The Queen", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-moves-to-capture-the-queen", - "solutionLink": "#" - }, - { - "problemName": "3002. Maximum Size of a Set After Removals", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximum-size-of-a-set-after-removals", - "solutionLink": "#" - }, - { - "problemName": "3003. Maximize the Number of Partitions After Operations", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations", - "solutionLink": "#" - }, - { - "problemName": "3004. Maximum Subtree of the Same Color", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximum-subtree-of-the-same-color", - "solutionLink": "#" - }, - { - "problemName": "3005. Count Elements With Maximum Frequency", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/count-elements-with-maximum-frequency", - "solutionLink": "#" - }, - { - "problemName": "3006. Find Beautiful Indices in the Given Array I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i", - "solutionLink": "#" - }, - { - "problemName": "3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k", - "solutionLink": "#" - }, - { - "problemName": "3008. Find Beautiful Indices in the Given Array II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-ii", - "solutionLink": "#" - }, - { - "problemName": "3009. Maximum Number of Intersections on the Chart", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/maximum-number-of-intersections-on-the-chart", - "solutionLink": "#" - }, - { - "problemName": "3010. Divide an Array Into Subarrays With Minimum Cost I", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i", - "solutionLink": "#" - }, - { - "problemName": "3011. Find if Array Can Be Sorted", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-if-array-can-be-sorted", - "solutionLink": "#" - }, - { - "problemName": "3012. Minimize Length of Array Using Operations", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimize-length-of-array-using-operations", - "solutionLink": "#" - }, - { - "problemName": "3013. Divide an Array Into Subarrays With Minimum Cost II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-ii", - "solutionLink": "#" - }, - { - "problemName": "3014. Minimum Number of Pushes to Type Word I", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i", - "solutionLink": "#" - }, - { - "problemName": "3015. Count the Number of Houses at a Certain Distance I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-i", - "solutionLink": "#" - }, - { - "problemName": "3016. Minimum Number of Pushes to Type Word II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii", - "solutionLink": "#" - }, - { - "problemName": "3017. Count the Number of Houses at a Certain Distance II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-ii", - "solutionLink": "#" - }, - { - "problemName": "3018. Maximum Number of Removal Queries That Can Be Processed I", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/maximum-number-of-removal-queries-that-can-be-processed-i", - "solutionLink": "#" - }, - { - "problemName": "3019. Number of Changing Keys", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/number-of-changing-keys", - "solutionLink": "#" - }, - { - "problemName": "3020. Find the Maximum Number of Elements in Subset", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-the-maximum-number-of-elements-in-subset", - "solutionLink": "#" - }, - { - "problemName": "3021. Alice and Bob Playing Flower Game", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/alice-and-bob-playing-flower-game", - "solutionLink": "#" - }, - { - "problemName": "3022. Minimize OR of Remaining Elements Using Operations", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/minimize-or-of-remaining-elements-using-operations", - "solutionLink": "#" - }, - { - "problemName": "3023. Find Pattern in Infinite Stream I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-pattern-in-infinite-stream-i", - "solutionLink": "#" - }, - { - "problemName": "3024. Type of Triangle", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/type-of-triangle", - "solutionLink": "#" - }, - { - "problemName": "3025. Find the Number of Ways to Place People I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-the-number-of-ways-to-place-people-i", - "solutionLink": "#" - }, - { - "problemName": "3026. Maximum Good Subarray Sum", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximum-good-subarray-sum", - "solutionLink": "#" - }, - { - "problemName": "3027. Find the Number of Ways to Place People II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/find-the-number-of-ways-to-place-people-ii", - "solutionLink": "#" - }, - { - "problemName": "3028. Ant on the Boundary", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/ant-on-the-boundary", - "solutionLink": "#" - }, - { - "problemName": "3029. Minimum Time to Revert Word to Initial State I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-i", - "solutionLink": "#" - }, - { - "problemName": "3030. Find the Grid of Region Average", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-the-grid-of-region-average", - "solutionLink": "#" - }, - { - "problemName": "3031. Minimum Time to Revert Word to Initial State II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-ii", - "solutionLink": "#" - }, - { - "problemName": "3032. Count Numbers With Unique Digits II", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/count-numbers-with-unique-digits-ii", - "solutionLink": "#" - }, - { - "problemName": "3033. Modify the Matrix", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/modify-the-matrix", - "solutionLink": "#" - }, - { - "problemName": "3034. Number of Subarrays That Match a Pattern I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i", - "solutionLink": "#" - }, - { - "problemName": "3035. Maximum Palindromes After Operations", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximum-palindromes-after-operations", - "solutionLink": "#" - }, - { - "problemName": "3036. Number of Subarrays That Match a Pattern II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-ii", - "solutionLink": "#" - }, - { - "problemName": "3037. Find Pattern in Infinite Stream II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/find-pattern-in-infinite-stream-ii", - "solutionLink": "#" - }, - { - "problemName": "3038. Maximum Number of Operations With the Same Score I", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i", - "solutionLink": "#" - }, - { - "problemName": "3039. Apply Operations to Make String Empty", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/apply-operations-to-make-string-empty", - "solutionLink": "#" - }, - { - "problemName": "3040. Maximum Number of Operations With the Same Score II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-ii", - "solutionLink": "#" - }, - { - "problemName": "3041. Maximize Consecutive Elements in an Array After Modification", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/maximize-consecutive-elements-in-an-array-after-modification", - "solutionLink": "#" - }, - { - "problemName": "3042. Count Prefix and Suffix Pairs I", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/count-prefix-and-suffix-pairs-i", - "solutionLink": "#" - }, - { - "problemName": "3043. Find the Length of the Longest Common Prefix", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix", - "solutionLink": "#" - }, - { - "problemName": "3044. Most Frequent Prime", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/most-frequent-prime", - "solutionLink": "#" - }, - { - "problemName": "3045. Count Prefix and Suffix Pairs II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/count-prefix-and-suffix-pairs-ii", - "solutionLink": "#" - }, - { - "problemName": "3046. Split the Array", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/split-the-array", - "solutionLink": "#" - }, - { - "problemName": "3047. Find the Largest Area of Square Inside Two Rectangles", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles", - "solutionLink": "#" - }, - { - "problemName": "3048. Earliest Second to Mark Indices I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/earliest-second-to-mark-indices-i", - "solutionLink": "#" - }, - { - "problemName": "3049. Earliest Second to Mark Indices II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/earliest-second-to-mark-indices-ii", - "solutionLink": "#" - }, - { - "problemName": "3050. Pizza Toppings Cost Analysis", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/pizza-toppings-cost-analysis", - "solutionLink": "#" - }, - { - "problemName": "3051. Find Candidates for Data Scientist Position", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/find-candidates-for-data-scientist-position", - "solutionLink": "#" - }, - { - "problemName": "3052. Maximize Items", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/maximize-items", - "solutionLink": "#" - }, - { - "problemName": "3053. Classifying Triangles by Lengths", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/classifying-triangles-by-lengths", - "solutionLink": "#" - }, - { - "problemName": "3054. Binary Tree Nodes", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/binary-tree-nodes", - "solutionLink": "#" - }, - { - "problemName": "3055. Top Percentile Fraud", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/top-percentile-fraud", - "solutionLink": "#" - }, - { - "problemName": "3056. Snaps Analysis", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/snaps-analysis", - "solutionLink": "#" - }, - { - "problemName": "3057. Employees Project Allocation", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/employees-project-allocation", - "solutionLink": "#" - }, - { - "problemName": "3058. Friends With No Mutual Friends", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/friends-with-no-mutual-friends", - "solutionLink": "#" - }, - { - "problemName": "3059. Find All Unique Email Domains", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/find-all-unique-email-domains", - "solutionLink": "#" - }, - { - "problemName": "3060. User Activities within Time Bounds", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/user-activities-within-time-bounds", - "solutionLink": "#" - }, - { - "problemName": "3061. Calculate Trapping Rain Water", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/calculate-trapping-rain-water", - "solutionLink": "#" - }, - { - "problemName": "3062. Winner of the Linked List Game", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/winner-of-the-linked-list-game", - "solutionLink": "#" - }, - { - "problemName": "3063. Linked List Frequency", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/linked-list-frequency", - "solutionLink": "#" - }, - { - "problemName": "3064. Guess the Number Using Bitwise Questions I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/guess-the-number-using-bitwise-questions-i", - "solutionLink": "#" - }, - { - "problemName": "3065. Minimum Operations to Exceed Threshold Value I", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-i", - "solutionLink": "#" - }, - { - "problemName": "3066. Minimum Operations to Exceed Threshold Value II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii", - "solutionLink": "#" - }, - { - "problemName": "3067. Count Pairs of Connectable Servers in a Weighted Tree Network", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network", - "solutionLink": "#" - }, - { - "problemName": "3068. Find the Maximum Sum of Node Values", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/find-the-maximum-sum-of-node-values", - "solutionLink": "#" - }, - { - "problemName": "3069. Distribute Elements Into Two Arrays I", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/distribute-elements-into-two-arrays-i", - "solutionLink": "#" - }, - { - "problemName": "3070. Count Submatrices with Top-Left Element and Sum Less Than k", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/count-submatrices-with-top-left-element-and-sum-less-than-k", - "solutionLink": "#" - }, - { - "problemName": "3071. Minimum Operations to Write the Letter Y on a Grid", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-operations-to-write-the-letter-y-on-a-grid", - "solutionLink": "#" - }, - { - "problemName": "3072. Distribute Elements Into Two Arrays II", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/distribute-elements-into-two-arrays-ii", - "solutionLink": "#" - }, - { - "problemName": "3073. Maximum Increasing Triplet Value", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximum-increasing-triplet-value", - "solutionLink": "#" - }, - { - "problemName": "3074. Apple Redistribution into Boxes", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/apple-redistribution-into-boxes", - "solutionLink": "#" - }, - { - "problemName": "3075. Maximize Happiness of Selected Children", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/maximize-happiness-of-selected-children", - "solutionLink": "#" - }, - { - "problemName": "3076. Shortest Uncommon Substring in an Array", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/shortest-uncommon-substring-in-an-array", - "solutionLink": "#" - }, - { - "problemName": "3077. Maximum Strength of K Disjoint Subarrays", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/maximum-strength-of-k-disjoint-subarrays", - "solutionLink": "#" - }, - { - "problemName": "3078. Match Alphanumerical Pattern in Matrix I", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/match-alphanumerical-pattern-in-matrix-i", - "solutionLink": "#" - }, - { - "problemName": "3079. Find the Sum of Encrypted Integers", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/find-the-sum-of-encrypted-integers", - "solutionLink": "#" - }, - { - "problemName": "3080. Mark Elements on Array by Performing Queries", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/mark-elements-on-array-by-performing-queries", - "solutionLink": "#" - }, - { - "problemName": "3081. Replace Question Marks in String to Minimize Its Value", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/replace-question-marks-in-string-to-minimize-its-value", - "solutionLink": "#" - }, - { - "problemName": "3082. Find the Sum of the Power of All Subsequences", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/find-the-sum-of-the-power-of-all-subsequences", - "solutionLink": "#" - }, - { - "problemName": "3083. Existence of a Substring in a String and Its Reverse", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse", - "solutionLink": "#" - }, - { - "problemName": "3084. Count Substrings Starting and Ending with Given Character", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character", - "solutionLink": "#" - }, - { - "problemName": "3085. Minimum Deletions to Make String K-Special", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-deletions-to-make-string-k-special", - "solutionLink": "#" - }, - { - "problemName": "3086. Minimum Moves to Pick K Ones", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/minimum-moves-to-pick-k-ones", - "solutionLink": "#" - }, - { - "problemName": "3087. Find Trending Hashtags", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-trending-hashtags", - "solutionLink": "#" - }, - { - "problemName": "3088. Make String Anti-palindrome", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/make-string-anti-palindrome", - "solutionLink": "#" - }, - { - "problemName": "3089. Find Bursty Behavior", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/find-bursty-behavior", - "solutionLink": "#" - }, - { - "problemName": "3090. Maximum Length Substring With Two Occurrences", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/maximum-length-substring-with-two-occurrences", - "solutionLink": "#" - }, - { - "problemName": "3091. Apply Operations to Make Sum of Array Greater Than or Equal to k", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k", - "solutionLink": "#" - }, - { - "problemName": "3092. Most Frequent IDs", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/most-frequent-ids", - "solutionLink": "#" - }, - { - "problemName": "3093. Longest Common Suffix Queries", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/longest-common-suffix-queries", - "solutionLink": "#" - }, - { - "problemName": "3094. Guess the Number Using Bitwise Questions II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/guess-the-number-using-bitwise-questions-ii", - "solutionLink": "#" - }, - { - "problemName": "3095. Shortest Subarray With OR at Least K I", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i", - "solutionLink": "#" - }, - { - "problemName": "3096. Minimum Levels to Gain More Points", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/minimum-levels-to-gain-more-points", - "solutionLink": "#" - }, - { - "problemName": "3097. Shortest Subarray With OR at Least K II", - "difficulty": "Medium", - "leetCodeLink": "https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii", - "solutionLink": "#" - }, - { - "problemName": "3098. Find the Sum of Subsequence Powers", - "difficulty": "Hard", - "leetCodeLink": "https://leetcode.com/problems/find-the-sum-of-subsequence-powers", - "solutionLink": "#" - }, - { - "problemName": "3099. Harshad Number", - "difficulty": "Easy", - "leetCodeLink": "https://leetcode.com/problems/harshad-number", - "solutionLink": "#" - } +{ +"problemName": "3000. Maximum Area of Longest Diagonal Rectangle", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle", +"solutionLink": "#" +}, +{ +"problemName": "3001. Minimum Moves to Capture The Queen", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-moves-to-capture-the-queen", +"solutionLink": "#" +}, +{ +"problemName": "3002. Maximum Size of a Set After Removals", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximum-size-of-a-set-after-removals", +"solutionLink": "#" +}, +{ +"problemName": "3003. Maximize the Number of Partitions After Operations", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/maximize-the-number-of-partitions-after-operations", +"solutionLink": "#" +}, +{ +"problemName": "3004. Maximum Subtree of the Same Color", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximum-subtree-of-the-same-color", +"solutionLink": "#" +}, +{ +"problemName": "3005. Count Elements With Maximum Frequency", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/count-elements-with-maximum-frequency", +"solutionLink": "#" +}, +{ +"problemName": "3006. Find Beautiful Indices in the Given Array I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i", +"solutionLink": "#" +}, +{ +"problemName": "3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximum-number-that-sum-of-the-prices-is-less-than-or-equal-to-k", +"solutionLink": "#" +}, +{ +"problemName": "3008. Find Beautiful Indices in the Given Array II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-ii", +"solutionLink": "#" +}, +{ +"problemName": "3009. Maximum Number of Intersections on the Chart", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/maximum-number-of-intersections-on-the-chart", +"solutionLink": "#" +}, +{ +"problemName": "3010. Divide an Array Into Subarrays With Minimum Cost I", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i", +"solutionLink": "#" +}, +{ +"problemName": "3011. Find if Array Can Be Sorted", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-if-array-can-be-sorted", +"solutionLink": "#" +}, +{ +"problemName": "3012. Minimize Length of Array Using Operations", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimize-length-of-array-using-operations", +"solutionLink": "#" +}, +{ +"problemName": "3013. Divide an Array Into Subarrays With Minimum Cost II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-ii", +"solutionLink": "#" +}, +{ +"problemName": "3014. Minimum Number of Pushes to Type Word I", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i", +"solutionLink": "#" +}, +{ +"problemName": "3015. Count the Number of Houses at a Certain Distance I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-i", +"solutionLink": "#" +}, +{ +"problemName": "3016. Minimum Number of Pushes to Type Word II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii", +"solutionLink": "#" +}, +{ +"problemName": "3017. Count the Number of Houses at a Certain Distance II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/count-the-number-of-houses-at-a-certain-distance-ii", +"solutionLink": "#" +}, +{ +"problemName": "3018. Maximum Number of Removal Queries That Can Be Processed I", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/maximum-number-of-removal-queries-that-can-be-processed-i", +"solutionLink": "#" +}, +{ +"problemName": "3019. Number of Changing Keys", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/number-of-changing-keys", +"solutionLink": "#" +}, +{ +"problemName": "3020. Find the Maximum Number of Elements in Subset", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-the-maximum-number-of-elements-in-subset", +"solutionLink": "#" +}, +{ +"problemName": "3021. Alice and Bob Playing Flower Game", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/alice-and-bob-playing-flower-game", +"solutionLink": "#" +}, +{ +"problemName": "3022. Minimize OR of Remaining Elements Using Operations", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/minimize-or-of-remaining-elements-using-operations", +"solutionLink": "#" +}, +{ +"problemName": "3023. Find Pattern in Infinite Stream I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-pattern-in-infinite-stream-i", +"solutionLink": "#" +}, +{ +"problemName": "3024. Type of Triangle", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/type-of-triangle", +"solutionLink": "#" +}, +{ +"problemName": "3025. Find the Number of Ways to Place People I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-the-number-of-ways-to-place-people-i", +"solutionLink": "#" +}, +{ +"problemName": "3026. Maximum Good Subarray Sum", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximum-good-subarray-sum", +"solutionLink": "#" +}, +{ +"problemName": "3027. Find the Number of Ways to Place People II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/find-the-number-of-ways-to-place-people-ii", +"solutionLink": "#" +}, +{ +"problemName": "3028. Ant on the Boundary", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/ant-on-the-boundary", +"solutionLink": "#" +}, +{ +"problemName": "3029. Minimum Time to Revert Word to Initial State I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-i", +"solutionLink": "#" +}, +{ +"problemName": "3030. Find the Grid of Region Average", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-the-grid-of-region-average", +"solutionLink": "#" +}, +{ +"problemName": "3031. Minimum Time to Revert Word to Initial State II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-ii", +"solutionLink": "#" +}, +{ +"problemName": "3032. Count Numbers With Unique Digits II", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/count-numbers-with-unique-digits-ii", +"solutionLink": "#" +}, +{ +"problemName": "3033. Modify the Matrix", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/modify-the-matrix", +"solutionLink": "#" +}, +{ +"problemName": "3034. Number of Subarrays That Match a Pattern I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-i", +"solutionLink": "#" +}, +{ +"problemName": "3035. Maximum Palindromes After Operations", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximum-palindromes-after-operations", +"solutionLink": "#" +}, +{ +"problemName": "3036. Number of Subarrays That Match a Pattern II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/number-of-subarrays-that-match-a-pattern-ii", +"solutionLink": "#" +}, +{ +"problemName": "3037. Find Pattern in Infinite Stream II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/find-pattern-in-infinite-stream-ii", +"solutionLink": "#" +}, +{ +"problemName": "3038. Maximum Number of Operations With the Same Score I", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-i", +"solutionLink": "#" +}, +{ +"problemName": "3039. Apply Operations to Make String Empty", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/apply-operations-to-make-string-empty", +"solutionLink": "#" +}, +{ +"problemName": "3040. Maximum Number of Operations With the Same Score II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximum-number-of-operations-with-the-same-score-ii", +"solutionLink": "#" +}, +{ +"problemName": "3041. Maximize Consecutive Elements in an Array After Modification", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/maximize-consecutive-elements-in-an-array-after-modification", +"solutionLink": "#" +}, +{ +"problemName": "3042. Count Prefix and Suffix Pairs I", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/count-prefix-and-suffix-pairs-i", +"solutionLink": "#" +}, +{ +"problemName": "3043. Find the Length of the Longest Common Prefix", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix", +"solutionLink": "#" +}, +{ +"problemName": "3044. Most Frequent Prime", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/most-frequent-prime", +"solutionLink": "#" +}, +{ +"problemName": "3045. Count Prefix and Suffix Pairs II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/count-prefix-and-suffix-pairs-ii", +"solutionLink": "#" +}, +{ +"problemName": "3046. Split the Array", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/split-the-array", +"solutionLink": "#" +}, +{ +"problemName": "3047. Find the Largest Area of Square Inside Two Rectangles", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles", +"solutionLink": "#" +}, +{ +"problemName": "3048. Earliest Second to Mark Indices I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/earliest-second-to-mark-indices-i", +"solutionLink": "#" +}, +{ +"problemName": "3049. Earliest Second to Mark Indices II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/earliest-second-to-mark-indices-ii", +"solutionLink": "#" +}, +{ +"problemName": "3050. Pizza Toppings Cost Analysis", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/pizza-toppings-cost-analysis", +"solutionLink": "#" +}, +{ +"problemName": "3051. Find Candidates for Data Scientist Position", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/find-candidates-for-data-scientist-position", +"solutionLink": "#" +}, +{ +"problemName": "3052. Maximize Items", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/maximize-items", +"solutionLink": "#" +}, +{ +"problemName": "3053. Classifying Triangles by Lengths", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/classifying-triangles-by-lengths", +"solutionLink": "#" +}, +{ +"problemName": "3054. Binary Tree Nodes", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/binary-tree-nodes", +"solutionLink": "#" +}, +{ +"problemName": "3055. Top Percentile Fraud", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/top-percentile-fraud", +"solutionLink": "#" +}, +{ +"problemName": "3056. Snaps Analysis", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/snaps-analysis", +"solutionLink": "#" +}, +{ +"problemName": "3057. Employees Project Allocation", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/employees-project-allocation", +"solutionLink": "#" +}, +{ +"problemName": "3058. Friends With No Mutual Friends", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/friends-with-no-mutual-friends", +"solutionLink": "#" +}, +{ +"problemName": "3059. Find All Unique Email Domains", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/find-all-unique-email-domains", +"solutionLink": "#" +}, +{ +"problemName": "3060. User Activities within Time Bounds", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/user-activities-within-time-bounds", +"solutionLink": "#" +}, +{ +"problemName": "3061. Calculate Trapping Rain Water", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/calculate-trapping-rain-water", +"solutionLink": "#" +}, +{ +"problemName": "3062. Winner of the Linked List Game", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/winner-of-the-linked-list-game", +"solutionLink": "#" +}, +{ +"problemName": "3063. Linked List Frequency", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/linked-list-frequency", +"solutionLink": "#" +}, +{ +"problemName": "3064. Guess the Number Using Bitwise Questions I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/guess-the-number-using-bitwise-questions-i", +"solutionLink": "#" +}, +{ +"problemName": "3065. Minimum Operations to Exceed Threshold Value I", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-i", +"solutionLink": "#" +}, +{ +"problemName": "3066. Minimum Operations to Exceed Threshold Value II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii", +"solutionLink": "#" +}, +{ +"problemName": "3067. Count Pairs of Connectable Servers in a Weighted Tree Network", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network", +"solutionLink": "#" +}, +{ +"problemName": "3068. Find the Maximum Sum of Node Values", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/find-the-maximum-sum-of-node-values", +"solutionLink": "#" +}, +{ +"problemName": "3069. Distribute Elements Into Two Arrays I", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/distribute-elements-into-two-arrays-i", +"solutionLink": "#" +}, +{ +"problemName": "3070. Count Submatrices with Top-Left Element and Sum Less Than k", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/count-submatrices-with-top-left-element-and-sum-less-than-k", +"solutionLink": "#" +}, +{ +"problemName": "3071. Minimum Operations to Write the Letter Y on a Grid", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-operations-to-write-the-letter-y-on-a-grid", +"solutionLink": "#" +}, +{ +"problemName": "3072. Distribute Elements Into Two Arrays II", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/distribute-elements-into-two-arrays-ii", +"solutionLink": "#" +}, +{ +"problemName": "3073. Maximum Increasing Triplet Value", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximum-increasing-triplet-value", +"solutionLink": "#" +}, +{ +"problemName": "3074. Apple Redistribution into Boxes", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/apple-redistribution-into-boxes", +"solutionLink": "#" +}, +{ +"problemName": "3075. Maximize Happiness of Selected Children", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/maximize-happiness-of-selected-children", +"solutionLink": "#" +}, +{ +"problemName": "3076. Shortest Uncommon Substring in an Array", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/shortest-uncommon-substring-in-an-array", +"solutionLink": "#" +}, +{ +"problemName": "3077. Maximum Strength of K Disjoint Subarrays", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/maximum-strength-of-k-disjoint-subarrays", +"solutionLink": "#" +}, +{ +"problemName": "3078. Match Alphanumerical Pattern in Matrix I", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/match-alphanumerical-pattern-in-matrix-i", +"solutionLink": "#" +}, +{ +"problemName": "3079. Find the Sum of Encrypted Integers", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/find-the-sum-of-encrypted-integers", +"solutionLink": "#" +}, +{ +"problemName": "3080. Mark Elements on Array by Performing Queries", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/mark-elements-on-array-by-performing-queries", +"solutionLink": "#" +}, +{ +"problemName": "3081. Replace Question Marks in String to Minimize Its Value", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/replace-question-marks-in-string-to-minimize-its-value", +"solutionLink": "#" +}, +{ +"problemName": "3082. Find the Sum of the Power of All Subsequences", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/find-the-sum-of-the-power-of-all-subsequences", +"solutionLink": "#" +}, +{ +"problemName": "3083. Existence of a Substring in a String and Its Reverse", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse", +"solutionLink": "#" +}, +{ +"problemName": "3084. Count Substrings Starting and Ending with Given Character", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character", +"solutionLink": "#" +}, +{ +"problemName": "3085. Minimum Deletions to Make String K-Special", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-deletions-to-make-string-k-special", +"solutionLink": "#" +}, +{ +"problemName": "3086. Minimum Moves to Pick K Ones", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/minimum-moves-to-pick-k-ones", +"solutionLink": "#" +}, +{ +"problemName": "3087. Find Trending Hashtags", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-trending-hashtags", +"solutionLink": "#" +}, +{ +"problemName": "3088. Make String Anti-palindrome", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/make-string-anti-palindrome", +"solutionLink": "#" +}, +{ +"problemName": "3089. Find Bursty Behavior", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/find-bursty-behavior", +"solutionLink": "#" +}, +{ +"problemName": "3090. Maximum Length Substring With Two Occurrences", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/maximum-length-substring-with-two-occurrences", +"solutionLink": "#" +}, +{ +"problemName": "3091. Apply Operations to Make Sum of Array Greater Than or Equal to k", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k", +"solutionLink": "#" +}, +{ +"problemName": "3092. Most Frequent IDs", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/most-frequent-ids", +"solutionLink": "#" +}, +{ +"problemName": "3093. Longest Common Suffix Queries", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/longest-common-suffix-queries", +"solutionLink": "#" +}, +{ +"problemName": "3094. Guess the Number Using Bitwise Questions II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/guess-the-number-using-bitwise-questions-ii", +"solutionLink": "#" +}, +{ +"problemName": "3095. Shortest Subarray With OR at Least K I", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i", +"solutionLink": "#" +}, +{ +"problemName": "3096. Minimum Levels to Gain More Points", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/minimum-levels-to-gain-more-points", +"solutionLink": "#" +}, +{ +"problemName": "3097. Shortest Subarray With OR at Least K II", +"difficulty": "Medium", +"leetCodeLink": "https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii", +"solutionLink": "#" +}, +{ +"problemName": "3098. Find the Sum of Subsequence Powers", +"difficulty": "Hard", +"leetCodeLink": "https://leetcode.com/problems/find-the-sum-of-subsequence-powers", +"solutionLink": "#" +}, +{ +"problemName": "3099. Harshad Number", +"difficulty": "Easy", +"leetCodeLink": "https://leetcode.com/problems/harshad-number", +"solutionLink": "/dsa-solutions/lc-solutions/3000-3099/Harshad-Number" +} ]; -Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. \ No newline at end of file +Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. diff --git a/dsa-solutions/lc-solutions/0400-0499/0451-Sort-Characters-By-Frequency.md b/dsa-solutions/lc-solutions/0400-0499/0451-Sort-Characters-By-Frequency.md index 5e16f7d9e..22757cc61 100644 --- a/dsa-solutions/lc-solutions/0400-0499/0451-Sort-Characters-By-Frequency.md +++ b/dsa-solutions/lc-solutions/0400-0499/0451-Sort-Characters-By-Frequency.md @@ -1,149 +1,123 @@ --- id: sort-characters-by-frequency -title: Sort Characters By Frequency -sidebar_label: 0451-Sort-Characters-By-Frequency +title: Sort Characters By Frequency (LeetCode) +sidebar_label: 0451-Sort Characters By Frequency tags: -- Hash Table -- String -- Sorting -- Heap (Priority Queue) -description: "Given a string `s`, sort it in decreasing order based on the frequency of characters, and return the sorted string." + - Hash Table + - String + - Sorting + - Heap(Priority Queue) + - Bucket Sort + - Counting +description: Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. +sidebar_position: 0451 --- -## Problem +## Problem Description -Given a string `s`, sort it in decreasing order based on the frequency of characters, and return the sorted string. +Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. -### Examples +Return the sorted string. If there are multiple answers, return any of them. -**Example 1:** +Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. -**Input:** `s = "tree"` -**Output:** `"eert"` -**Explanation:** 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before 'r' and 't'. Therefore "eetr" is also a valid answer. +### Example 1 -**Example 2:** +- **Input:** ` s = "tree" ` +- **Output:** `"eert"` +- **Explanation:** 'e' appears twice while 'r' and 't' both appear once.So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. -**Input:** `s = "cccaaa"` -**Output:** `"cccaaa"` -**Explanation:** Both 'c' and 'a' appear three times. So "aaaccc" is also a valid answer. Note that "cacaca" is incorrect, as same characters must be together. +### Example 2 -**Example 3:** +- **Input:** ` s = "cccaaa" ` +- **Output:** `"aaaccc"` +- **Explanation:** Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.Note that "cacaca" is incorrect, as the same characters must be together. -**Input:** `s = "Aabb"` -**Output:** `"bbAa"` -**Explanation:** 'b' appears twice, 'A' and 'a' both appear once. Note that 'A' and 'a' are treated as two different characters. +### Example 3 -### Constraints +- **Input:** ` s = "Aabb" ` +- **Output:** `"bbAa"` +- **Explanation:** "bbaA" is also a valid answer, but "Aabb" is incorrect.Note that 'A' and 'a' are treated as two different characters. -- `1 <= s.length <= 5 * 10^5` -- `s` consists of uppercase and lowercase English letters and digits. ---- -## Approach +### Constraints -To solve this problem, we can follow these steps: +- `1 <= s.length <= 5 * 105` +- `s consists of uppercase and lowercase English letters and digits.` -1. **Count Frequencies:** Use a hash table (dictionary) to count the frequency of each character in the string. -2. **Sort Characters:** Create a list of characters sorted by their frequency in descending order. -3. **Build Result String:** Construct the result string by repeating characters based on their frequencies. +## Approach -### Steps: + - 1. Count the occurrences: We need to go through the string and count the occurrences of each character. This can be done efficiently by using a hash table or a counter data structure where each character is a key, and its count is the value. + + - 2. Sort based on counts: Once we have the counts, the next step is to sort the characters by these counts in descending order. We want the characters with higher counts to come first. + + - 3. With these counts, we can construct a new string. -1. Create a hash table to store the frequency of each character. -2. Sort the characters based on their frequency in descending order. -3. Build the final string by appending each character multiplied by its frequency. -### Solution -#### Java Solution +### Solution Code -```java -import java.util.HashMap; -import java.util.Map; -import java.util.PriorityQueue; - -class Solution { - public String frequencySort(String s) { - Map frequencyMap = new HashMap<>(); - for (char c : s.toCharArray()) { - frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1); - } - - PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> frequencyMap.get(b) - frequencyMap.get(a)); - maxHeap.addAll(frequencyMap.keySet()); - - StringBuilder result = new StringBuilder(); - while (!maxHeap.isEmpty()) { - char c = maxHeap.poll(); - int count = frequencyMap.get(c); - for (int i = 0; i < count; i++) { - result.append(c); - } - } - - return result.toString(); - } -} -``` -### C++ Solution - -```cpp -#include -#include -#include +#### C++ +```c++ class Solution { public: string frequencySort(string s) { unordered_map frequencyMap; - for (char c : s) { - frequencyMap[c]++; + for (char ch : s) { + ++frequencyMap[ch]; } - - priority_queue> maxHeap; - for (auto& [c, freq] : frequencyMap) { - maxHeap.push({freq, c}); + vector uniqueChars; + for (auto& keyValue : frequencyMap) { + uniqueChars.push_back(keyValue.first); } - + sort(uniqueChars.begin(), uniqueChars.end(), [&](char a, char b) { + return frequencyMap[a] > frequencyMap[b]; + }); string result; - while (!maxHeap.empty()) { - auto [freq, c] = maxHeap.top(); - maxHeap.pop(); - result.append(freq, c); + for (char ch : uniqueChars) { + result += string(frequencyMap[ch], ch); } - return result; } }; + ``` -### Python Solution +#### java +```java +class Solution { + public String frequencySort(String s) { + Map frequencyMap = new HashMap<>(52); + for (int i = 0; i < s.length(); ++i) { + frequencyMap.merge(s.charAt(i), 1, Integer::sum); + } + List characters = new ArrayList<>(frequencyMap.keySet()); + characters.sort((a, b) -> frequencyMap.get(b) - frequencyMap.get(a)); + StringBuilder sortedString = new StringBuilder(); + for (char c : characters) { + for (int frequency = frequencyMap.get(c); frequency > 0; --frequency) { + sortedString.append(c); + } + } + return sortedString.toString(); + } +} + +``` +#### Python ```python -from collections import Counter -import heapq - class Solution: def frequencySort(self, s: str) -> str: - frequency_map = Counter(s) - max_heap = [(-freq, char) for char, freq in frequency_map.items()] - heapq.heapify(max_heap) - - result = [] - while max_heap: - freq, char = heapq.heappop(max_heap) - result.append(char * -freq) - - return ''.join(result) + char_frequency = Counter(s) + sorted_characters = sorted(char_frequency.items(), key=lambda item: -item[1]) + frequency_sorted_string = ''.join(character * frequency for character, frequency in sorted_characters) + return frequency_sorted_string ``` -### Complexity Analysis -**Time Complexity:** O(n log n) ->Reason: Counting the frequency of each character takes O(n), and sorting the characters based on frequency takes O(n log n). - -**Space Complexity:** O(n) ->Reason: The space needed for the frequency map, heap, and result string is proportional to the length of the input string. -### References -LeetCode Problem: Sort Characters By Frequency +#### Conclusion +The above solutions use two-pointers approach to reverse the string. + - 1. Time complexity: O(n logn) + - 2. Space complexity: O(n) diff --git a/dsa-solutions/lc-solutions/3000-3099/3099-Harshad-Number.md b/dsa-solutions/lc-solutions/3000-3099/3099-Harshad-Number.md new file mode 100644 index 000000000..b79a33e0d --- /dev/null +++ b/dsa-solutions/lc-solutions/3000-3099/3099-Harshad-Number.md @@ -0,0 +1,133 @@ +--- +id: Harshad-Number +title: Harshad-Number +sidebar_label: 3099 -Harshad-Number +tags: + - Array + - Sorting + - Enumeration +description: "This is a solution to the Harshad Number problem on LeetCode." +--- + +## Problem statement: + +An integer divisible by the sum of its digits is said to be a Harshad number. You are given an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1. + +**Example 1:** + +Input: x = 18 +Output: 9 + +Explanation: +The sum of digits of x is 9. 18 is divisible by 9. So 18 is a Harshad number and the answer is 9. + +**Example 2:** + +Input: x = 23 +Output: -1 + +Explanation: +The sum of digits of x is 5. 23 is not divisible by 5. So 23 is not a Harshad number and the answer is -1. + +Constraints: +`1 <= x <= 100` + +## Solutions: + +### Intuition + +When faced with the problem of identifying whether a number is a Harshad number (also known as a Niven number), the initial thought is to break down the number into its individual digits, sum those digits, and then check if the original number is divisible by the sum of its digits without a remainder. This process involves basic arithmetic operations and iterating over each digit of the number. + +### Approach + + 1. **Summing the Digits**: First, we need a method to sum the digits of the given number. This can be done by repeatedly dividing the number by 10 and adding the remainder to a sum variable until the number becomes `0`. + + 2. **Checking Divisibility**: Once we have the sum of the digits, we check if the original number is divisible by this sum. If the modulus (%) operation of the number divided by the sum of its digits equals 0, it is a Harshad number; otherwise, it is not. + + 3. **Returning the Result**: If the number is a Harshad number, we return the sum of its digits as specified in the problem statement. If it's not, we return -1 (or an appropriate value indicating it's not a Harshad number). + +## Code + + + + + ```cpp + class Solution { + public: + int sumOfTheDigitsOfHarshadNumber(int x) { + int sum = 0; + int n = x; + + while(n != 0){ + sum = sum + n % 10; + n = n/10; + } + + if(x % sum == 0){ + return sum; + } + return -1; + } + }; + ``` + + + + ```java + class Solution { + public int sumOfTheDigitsOfHarshadNumber(int x) { + int sum = 0; + int a = x; + while (a > 0) { + sum += a % 10; + a /= 10; + } + if (x % sum == 0) + return sum; + return -1; + } + } + ``` + + + + ```python + class Solution(object): + def sumOfTheDigitsOfHarshadNumber(self, x): + sum_number = 0 + temp = x + while temp > 0: + sum_number += temp % 10 + temp //= 10 + if x % sum_number == 0: + return sum_number + return -1 + ``` + + + + ```c + int sumOfTheDigitsOfHarshadNumber(int x) { + int sumNumber = 0; + int temp = x; + while (temp) { + sumNumber += temp % 10; + temp /= 10; + } + if (x % sumNumber == 0) + return sumNumber; + return -1; + } + ``` + + + +## Complexity + +### Time complexity: + +The time complexity is $O(k)$, where k is the number of digits in the number. This is because we need to iterate over each digit to sum them, and the number of iterations is proportional to the number of digits in the number. + +### Space complexity: + +The space complexity is $O(1)$, as the space required does not grow with the input size. We only need a fixed amount of variables to store the sum of the digits, the temporary value of the number as we reduce it, and possibly the original number if we need to return the sum of its digits.