Minimum Length of String After Deleting Similar Ends, 1751. Maximum Alternating Subsequence Sum, 1913. Number of Distinct Binary Strings After Applying Operations, 2452. Find the Student that Will Replace the Chalk, 1896. Minimum Number of Flips to Make the Binary String Alternating, 1889. Minimize the Total Price of the Trips, 2654. Number of Subsequences That Satisfy the Given Sum Condition, 1502. Number of Ways Where Square of Number Is Equal to Product of Two Numbers, 1579. Maximum Number of Non-overlapping Palindrome Substrings, 2474. Count Substrings Without Repeating Character, 2744. Execute Asynchronous Functions in Parallel, 2730. Find the Student that Will Replace the Chalk, 1896. Minimum Initial Energy to Finish Tasks, 1671. 3 characters in a row in the string), such that these substring dont contain duplicate characters. Sum of Digits of String After Convert, 1946. Split Array into Consecutive Subsequences, 668. Shortest Path in a Grid with Obstacles Elimination, 1295. Longest Substring Of All Vowels in Order, 1846. Categorize Box According to Criteria, 2526. Minimum Time to Remove All Cars Containing Illegal Goods, 2168. Find Minimum in Rotated Sorted Array II, 158. Number of Students Unable to Eat Lunch, 1703. Make Number of Distinct Characters Equal, 2535. Number of Students Doing Homework at a Given Time, 1452. Labeling layer with two attributes in QGIS, Adding salt pellets direct to home water tank, Three equations with a common positive root. Minimum Cost to Reach City With Discounts, 2095. Product of Two Run-Length Encoded Arrays, 1869. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Power of Four. Minimum Number of Operations to Make Array Continuous, 2010. Minimum Total Cost to Make Arrays Unequal, 2503. System Design. Reverse Substrings Between Each Pair of Parentheses, 1196. Sum of Imbalance Numbers of All Subarrays, 2764. is Array a Preorder of Some Binary Tree, 2767. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K, 1415. Minimum Amount of Time to Fill Cups, 2340. Binary Tree Level Order Traversal II, 108. Change Minimum Characters to Satisfy One of Three Conditions, 1738. Count Number of Ways to Place Houses, 2322. Minimum Space Wasted From Packaging, 1893. Minimum Operations to Make Array Equal, 1553. Remove Colored Pieces if Both Neighbors are the Same Color, 2039. Eliminate Maximum Number of Monsters, 1928. The Users That Are Eligible for Discount, 2231. Find the Divisibility Array of a String, 2576. Count Elements With Strictly Smaller and Greater Elements, 2150. Find Nearest Point That Has the Same X or Y Coordinate, 1780. Construct Binary Search Tree from Preorder Traversal, 1010. Minimum Score After Removals on a Tree, 2323. Ways to Split Array Into Three Subarrays, 1713. Minimum Operations to Convert Number, 2060. Make Costs of Paths Equal in a Binary Tree, 2682. Number of Spaces Cleaning Robot Cleaned, 2064. Find Minimum Time to Finish All Jobs II, 2328. Check if All the Integers in a Range Are Covered, 1894. Number of Ways to Stay in the Same Place After Some Steps, 1270. Delete Characters to Make Fancy String, 1959. Count Pairs of Equal Substrings With Minimum Difference, 1798. Maximum Number of Groups Getting Fresh Donuts, 1819. Count All Valid Pickup and Delivery Options, 1364. Pairs of Songs With Total Durations Divisible by 60, 1011. Minimum Add to Make Parentheses Valid, 945. Maximum XOR of Two Non-Overlapping Subtrees, 2482. Maximum Candies You Can Get from Boxes, 1299. Unique Orders and Customers Per Month, 1566. Least Number of Unique Integers after K Removals, 1482. Execute Cancellable Function With Delay, 2721. Maximum Number of Groups Entering a Competition, 2359. People Whose List of Favorite Companies Is Not a Subset of Another List, 1453. Minimum Time to Remove All Cars Containing Illegal Goods, 2168. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix, 1285. Find the Start and End Number of Continuous Ranges, 1287. Number of Submatrices That Sum to Target, 1080. The Time When the Network Becomes Idle, 2040. Frequency of the Most Frequent Element, 1839. Traffic Light Controlled Intersection, 1281. Determine Color of a Chessboard Square, 1815. Guess the Majority in a Hidden Array, 1541. Minimum Space Wasted From Packaging, 1893. Remove Zero Sum Consecutive Nodes from Linked List, 1178. Check If All 1's Are at Least Length K Places Away, 1438. Largest 3-Same-Digit Number in String, 2265. Longest Subsequence Repeated k Times, 2015. Minimum Moves to Move a Box to Their Target Location, 1265. Friend Requests II: Who Has the Most Friends, 615. All Valid Triplets That Can Represent a Country, 1624. Replace Elements with Greatest Element on Right Side, 1300. Circular Permutation in Binary Representation, 1239. This is the best place to expand your knowledge and get prepared for your next interview. Flip Columns For Maximum Number of Equal Rows, 1074. Find the Punishment Number of an Integer, 2701. I know how to build one in O(n^2), but not in O(n). Check if One String Swap Can Make Strings Equal, 1794. Maximum Number of Books You Can Take, 2356. Number of Pairs of Strings With Concatenation Equal to Target, 2025. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts, 1466. Longest Continuous Increasing Subsequence, 689. Subsequence of Size K With the Largest Even Sum, 2099. Flatten a Multilevel Doubly Linked List, 440. The k-th Lexicographical String of All Happy Strings of Length n, 1418. Number of Burgers with No Waste of Ingredients, 1277. Minimum Number of Days to Eat N Oranges, 1557. Minimize Hamming Distance After Swap Operations, 1723. Maximum Element After Decreasing and Rearranging, 1848. Lowest Common Ancestor of a Binary Tree II, 1647. Closest Nodes Queries in a Binary Search Tree, 2477. Largest Positive Integer That Exists With Its Negative, 2442. 2550. Maximum XOR of Two Numbers in an Array, 423. Number of Subarrays With GCD Equal to K, 2449. Number of People That Can Be Seen in a Grid, 2283. Check If a String Contains All Binary Codes of Size K, 1464. Minimum Number of Operations to Convert Time, 2225. Minimum Number of Lines to Cover Points, 2153. Delete N Nodes After M Nodes of a Linked List, 1475. Number of Times a Driver Was a Passenger, 2240. Largest Submatrix With Rearrangements, 1731. Number of Strings That Appear as Substrings in Word, 1968. Check If Array Pairs Are Divisible by k, 1498. Minimum Operations to Make a Uni-Value Grid, 2035. Check if Every Row and Column Contains All Numbers, 2134. Customers with Maximum Number of Transactions on Consecutive Days, 2753. Determine if Two Events Have Conflict, 2447. Count Number of Distinct Integers After Reverse Operations, 2446. Removing Minimum Number of Magic Beans, 2174. Difference Between Ones and Zeros in Row and Column, 2486. Swap For Longest Repeated Character Substring, 1157. Difference of Number of Distinct Values on Diagonals, 2712. Paths in Matrix Whose Sum Is Divisible by K, 2436. Maximum XOR With an Element From Array, 1712. Find the Start and End Number of Continuous Ranges, 1287. All People Report to the Given Manager, 1276. Longest Subsequence With Limited Sum, 2391. Apply Bitwise Operations to Make Strings Equal, 2554. Minimum Operations to Reduce an Integer to 0, 2572. Leftmost Column with at Least a One, 1430. 1. T<=20; Each test case consists of one string, whose length is <= 1000 Output Smallest Subsequence of Distinct Characters, 1085. Maximum Number of Accepted Invitations, 1821. Maximum Cost of Trip With K Highways, 2249. Successful Pairs of Spells and Potions, 2302. Minimum ASCII Delete Sum for Two Strings, 714. Find the Quiet Students in All Exams, 1413. Verify Preorder Sequence in Binary Search Tree, 297. Minimum Adjacent Swaps to Reach the Kth Smallest Number, 1851. Number of Substrings With Fixed Ratio, 2491. Longest Subsequence Repeated k Times, 2015. Step-By-Step Directions From a Binary Tree Node to Another, 2098. List the Products Ordered in a Period, 1330. Partition Array into Disjoint Intervals, 921. Maximum Employees to Be Invited to a Meeting, 2128. Max Dot Product of Two Subsequences, 1460. Make Costs of Paths Equal in a Binary Tree, 2682. 1697. Median of Two Sorted Arrays 5. Maximum Sum With Exactly K Elements, 2657. Minimum Operations to Make the Array K-Increasing, 2113. Find the Subtasks That Did Not Execute, 1769. Add Two Polynomials Represented as Linked Lists, 1637. Largest 3-Same-Digit Number in String, 2265. Maximum Score Words Formed by Letters, 1261. Find the City With the Smallest Number of Neighbors at a Threshold Distance, 1335. Remove Colored Pieces if Both Neighbors are the Same Color, 2039. Count Subarrays With More Ones Than Zeros, 2033. Find Positive Integer Solution for a Given Equation, 1238. The First Day of the Maximum Recorded Degree in Each City, 2316. Count Artist Occurrences On Spotify Ranking List, 2672. Minimum Difficulty of a Job Schedule, 1339. Largest Number After Digit Swaps by Parity, 2232. Count Odd Numbers in an Interval Range, 1525. Number Of Rectangles That Can Form The Largest Square, 1727. Count Hills and Valleys in an Array, 2212. Friend Requests I: Overall Acceptance Rate, 600. Divide a String Into Groups of Size k, 2139. Minimum Consecutive Cards to Pick Up, 2263. Time Complexity: O(n2), where n is the length of string.Auxiliary Space: O(n). Maximum Element After Decreasing and Rearranging, 1848. Maximum Number of Ways to Partition an Array, 2030. Lowest Common Ancestor of Deepest Leaves, 1129. Sum of Imbalance Numbers of All Subarrays, 2764. is Array a Preorder of Some Binary Tree, 2767. Longest Common Subsequence Between Sorted Arrays, 1941. Maximum Subarray Sum After One Operation, 1749. Partition String Into Substrings With Values at Most K, 2524. Number of Longest Increasing Subsequence, 674. Painting a Grid With Three Different Colors, 1933. Consecutive Numbers Sum . Remove Duplicates from Sorted Array II, 82. Remove Stones to Minimize the Total, 1963. Choose Edges to Maximize Score in a Tree, 2379. Smallest Range Covering Elements from K Lists, 659. wxyz, wxy, wx, w, xyz, xy, x, yz, y, z so their count is 10. Find Substring With Given Hash Value, 2158. The number of substrings of length 3 is n 2, that is O(n). Maximum Number of Jumps to Reach the Last Index, 2771. Find The Original Array of Prefix Xor, 2434. Populating Next Right Pointers in Each Node, 117. Check if Binary String Has at Most One Segment of Ones, 1785. Check If String Is a Prefix of Array, 1962. Introduction. The number of substrings of s is then the number of prefixes of strings in the trie, which you can calculate simply in linear time. Find Original Array From Doubled Array, 2009. Maximum Number of Consecutive Values You Can Make, 1802. Serialize and Deserialize Binary Tree, 298. However, the result will still be O(n), since I gather you will need at the very least n/2 2 comparisons if all the characters of the string are the same. Maximum Number of Weeks for Which You Can Work, 1954. Count Strictly Increasing Subarrays, 2398. Check if Number is a Sum of Powers of Three, 1784. Partitioning Into Minimum Number Of Deci-Binary Numbers, 1697. Widest Vertical Area Between Two Points Containing No Points, 1638. Customers Who Bought Products A and B but Not C, 1403. Count Square Submatrices with All Ones, 1279. Maximum XOR With an Element From Array, 1712. Execution of All Suffix Instructions Staying in a Grid, 2121. Check if All Characters Have Equal Number of Occurrences, 1942. Can You Eat Your Favorite Candy on Your Favorite Day? A substring of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string. Take K of Each Character From Left and Right, 2520. Remove Trailing Zeros From a String, 2711. Number of Connected Components in an Undirected Graph, 331. K Highest Ranked Items Within a Price Range, 2147. Total Count of the distinct substrings is 9 + 1 (for empty string) = 10. You can use Ukkonen's algorithm to build a suffix tree in linear time: https://en.wikipedia.org/wiki/Ukkonen%27s_algorithm. Check if a Parentheses String Can Be Valid, 2117. Longest Even Odd Subarray With Threshold, 2763. Find the Maximum Divisibility Score, 2645. What is the motivation for infinity category theory? The Number of Users That Are Eligible for Discount, 2207. Maximum Alternating Subsequence Sum, 1913. Longest Substring of One Repeating Character, 2216. Maximum Number of Points From Grid Queries, 2504. Minimize Rounding Error to Meet Target, 1059. Connecting Cities With Minimum Cost, 1141. Minimum Operations to Make the Array Increasing, 1828. Substring with Concatenation of All Words, 34. Count Positions on Street With Required Brightness, 2238. Insufficient Nodes in Root to Leaf Paths, 1081. Minimum Score Triangulation of Polygon, 1047. Partition String Into Minimum Beautiful Substrings, 2770. Maximum White Tiles Covered by a Carpet, 2273. 0342. Minimum Operations to Remove Adjacent Ones in Matrix, 2124. Longest Substring with At Most Two Distinct Characters, 181. Count Ways to Make Array With Product, 1736. leetcode. Remove Duplicates From an Unsorted Linked List, 1838. The basic idea behind this approach is to use a HashSet 'ans' to store all the possible substrings generated from the string 'WORD'. Kth Smallest Number in Multiplication Table, 671. Search K. Count the Number of Square-Free Subsets, 2575. Count number of substrings Medium Accuracy: 20.46% Submissions: 28K+ Points: 4 Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Length of the Longest Alphabetical Continuous Substring, 2419. Find First and Last Position of Element in Sorted Array, 80. Minimum Flips in Binary Tree to Get Result, 2314. Minimize Hamming Distance After Swap Operations, 1723. Find Positive Integer Solution for a Given Equation, 1238. Construct the LCP array and subtract its sum from the number of substrings (n(n+1)/2). Find All Lonely Numbers in the Array, 2151. Delivering Boxes from Storage to Ports, 1689. The Time When the Network Becomes Idle, 2040. Merge Overlapping Events in the Same Hall, 2495. Lowest Common Ancestor of a Binary Tree, 255. Maximum Running Time of N Computers, 2142. Remove Duplicates from Sorted List II, 103. Keep Multiplying Found Values by Two, 2155. LeetCode / Count number of substrings with exactly k distinct characters.cpp Go to file Go to file T; Go to line L; Copy path Copy permalink; . Divide Array in Sets of K Consecutive Numbers, 1297. Maximum Number of Accepted Invitations, 1821. Longest Word in Dictionary through Deleting, 549. Maximum Candies You Can Get from Boxes, 1299. Minimum Number of Visited Cells in a Grid, 2635. Divide Intervals Into Minimum Number of Groups, 2410. Maximum Split of Positive Even Integers, 2184. Sell Diminishing-Valued Colored Balls, 1649. Convert Sorted List to Binary Search Tree, 116. Minimum Changes To Make Alternating Binary String, 1759. Substrings That Begin and End With the Same Letter, 2084. Lexicographically Smallest Equivalent String, 1072. Maximum Fruits Harvested After at Most K Steps, 2107. Smallest Missing Genetic Value in Each Subtree, 2004. Number of Smooth Descent Periods of a Stock, 2111. Amount of New Area Painted Each Day, 2160. Longest Binary Subsequence Less Than or Equal to K, 2313. Convert Sorted Array to Binary Search Tree, 109. Maximum Number of Non-Overlapping Substrings, 1521. Maximum Number of Events That Can Be Attended II, 1752. Minimum Number of Days to Eat N Oranges, 1557. Number of Restricted Paths From First to Last Node, 1787. Largest Number After Mutating Substring, 1950. Smallest Value After Replacing With Sum of Prime Factors, 2508. The Score of Students Solving Math Expression, 2020. Count Number of Rectangles Containing Each Point, 2259. Maximum Points You Can Obtain from Cards, 1428. Minimum Number of Food Buckets to Feed the Hamsters, 2087. Merge Overlapping Events in the Same Hall, 2495. Number of Students Doing Homework at a Given Time, 1452. Please consume this content on nados.pepcoding.com for a richer experience. Groups of Special-Equivalent Strings, 915. SDE-Sheet/Linkedin/Telegram/Instagram: https://linktr.ee/takeUforward-------------------------------------------------------------------------------------------------------------Problem Link: https://bit.ly/3ocRQW0 Count Pairs of Equal Substrings With Minimum Difference, 1798. Add Two Numbers. acknowledge that you have read and understood our. Friend Requests I: Overall Acceptance Rate, 600. Maximum Number of Consecutive Values You Can Make, 1802. Restore the Array From Adjacent Pairs. First Day Where You Have Been in All the Rooms, 1999. How do I make the first letter of a string uppercase in JavaScript? Maximum Good People Based on Statements, 2152. Find the Subtasks That Did Not Execute, 1769. A substring of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string. Find Minimum in Rotated Sorted Array II, 158. Smallest Subsequence of Distinct Characters, 1085. Count Substrings with Only One Distinct Letter.py Go to file Cannot retrieve contributors at this time 48 lines (40 sloc) 984 Bytes Raw Blame """ Given a string S, return the number of substrings that have only one distinct letter. Number of Visible People in a Queue, 1945. Maximum Difference by Remapping a Digit, 2567. Count Subtrees With Max Distance Between Cities, 1618. Maximum Non Negative Product in a Matrix, 1595. Minimum Insertions to Balance a Parentheses String, 1546. Maximize Total Tastiness of Purchased Fruits, 2432. Frequency of the Most Frequent Element, 1839. Find Elements in a Contaminated Binary Tree, 1263. Element Appearing More Than 25% In Sorted Array, 1290. Longest Repeating Character Replacement, 426. Generate a String With Characters That Have Odd Counts, 1375. Change Null Values in a Table to the Previous Value, 2389. Examples: The idea is create a Trie of all suffixes of given string. Verify Preorder Serialization of a Binary Tree, 340. rev2023.7.17.43537. Minimum Degree of a Connected Trio in a Graph, 1764. Get the Second Most Recent Activity, 1371. Logical OR of Two Binary Grids Represented as Quad-Trees, 562. Check Whether Two Strings are Almost Equivalent, 2071. The Category of Each Member in the Store, 2052. Check if Word Equals Summation of Two Words, 1883. Find Subsequence of Length K With the Largest Sum, 2106. Greatest English Letter in Upper and Lower Case, 2311. Replace Elements with Greatest Element on Right Side, 1300. Sort Array by Moving Items to Empty Space, 2461. Find the Maximum Divisibility Score, 2645. Find First Palindromic String in the Array, 2110. Find N Unique Integers Sum up to Zero, 1305. The Number of Seniors and Juniors to Join the Company, 2005. Find Elements in a Contaminated Binary Tree, 1263. Number of Ways to Build House of Cards, 2190. Minimum Sum of Four Digit Number After Splitting Digits, 2161. Number of Sets of K Non-Overlapping Line Segments, 1623. Maximum Number of People That Can Be Caught in Tag, 1996. Add Edges to Make Degrees of All Nodes Even, 2510. Maximum Number of Removable Characters, 1899. Count Subtrees With Max Distance Between Cities, 1618. Amount of New Area Painted Each Day, 2160. Remove Max Number of Edges to Keep Graph Fully Traversable, 1581. Maximum Points You Can Obtain from Cards, 1428. Minimum Cost to Reach City With Discounts, 2095. Smallest String With A Given Numeric Value, 1665. Minimum Time to Collect All Apples in a Tree, 1449. Minimum Remove to Make Valid Parentheses, 1255. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix, 1285. Number of Pairs Satisfying Inequality, 2431. Maximum Number of Occurrences of a Substring, 1298. Insert into a Sorted Circular Linked List, 712. Minimum Adjacent Swaps to Make a Valid Array, 2342. How Many Numbers Are Smaller Than the Current Number, 1368. Can You Eat Your Favorite Candy on Your Favorite Day? Create Target Array in the Given Order, 1391. K-th Smallest in Lexicographical Order, 448. Full version of this LeetCode solution available on my Udemy course (100 LeetCode videos):https://www.udemy.com/course/foolproof-leetcode-coding-interview-solutions-in-python/?couponCode=TECHZOO #pythonFree Background Music No Copyright Music DM Galaxy - Etiquette (Future Bass): https://click.dj/freebackgroundmusic/dm-galaxy-etiquette-original-mix-free-background-music-no-copyright-musicArtist: DM GalaxyYouTube - goo.gl/5Tezt7Facebook - www.facebook.com/DmGalaxySoundCloud - @dmgalaxyFree Background MusicWebsite - www.freebackgroundmusic.co.ukYouTube - goo.gl/fFI0z8SoundCloud - @freebmusicFacebook - goo.gl/2otgOUTwitter - twitter.com/FreeBMusicGoogle+ - goo.gl/TybZuK Check if There is a Valid Partition For The Array, 2372. Calculate the Influence of Each Salesperson, 2375. The Number of Weak Characters in the Game, 1997. 3. User Activity for the Past 30 Days I, 1142. Largest Positive Integer That Exists With Its Negative, 2442. Letter Combinations of a Phone Number, 28. Lexicographically Smallest Palindrome, 2698. Second Minimum Node In a Binary Tree, 673. Sort Items by Groups Respecting Dependencies, 1209. Minimum Total Space Wasted With K Resizing Operations, 1960. Check for Contradictions in Equations, 2309. Sort Even and Odd Indices Independently, 2165. Maximum Consecutive Floors Without Special Floors, 2275. Sort Even and Odd Indices Independently, 2165. Ways to Split Array Into Good Subarrays, 2752. Partition Array into Disjoint Intervals, 921. The Most Recent Orders for Each Product, 1551. Minimize Result by Adding Parentheses to Expression, 2234. Minimum Total Space Wasted With K Resizing Operations, 1960. Minimum Cost to Reach Destination in Time, 1930. Equal Sum Arrays With Minimum Number of Operations, 1779. Minimum Cost to Separate Sentence Into Rows, 2056. Random Point in Non-overlapping Rectangles, 524. Minimum ASCII Delete Sum for Two Strings, 714. Customer Who Visited but Did Not Make Any Transactions, 1582. Check If a String Can Break Another String, 1434. Minimum Number of Moves to Make Palindrome, 2196. Probability of a Two Boxes Having The Same Number of Distinct Balls, 1474. Find the Winner of the Circular Game, 1827. Largest Combination With Bitwise AND Greater Than Zero, 2279. Remove Max Number of Edges to Keep Graph Fully Traversable, 1581. Maximize Total Tastiness of Purchased Fruits, 2432. Sum of Digits of String After Convert, 1946. Longest Repeating Character Replacement, 426. String Transforms Into Another String, 1155. Minimum Adjacent Swaps for K Consecutive Ones, 1704. Minimum Number of Days to Disconnect Island, 1569. Find Greatest Common Divisor of Array, 1981. Maximum Fruits Harvested After at Most K Steps, 2107. Maximum Value of a String in an Array, 2499. Minimum Moves to Move a Box to Their Target Location, 1265. Maximum Number of Achievable Transfer Requests, 1602. Longest Subarray With Maximum Bitwise AND, 2422. The Number of the Smallest Unoccupied Chair, 1944. Minimum Bit Flips to Convert Number, 2224. Find All Possible Recipes from Given Supplies, 2116. Full version of this LeetCode solution available on my Udemy course (100 LeetCode videos):https://www.udemy.com/course/foolproof-leetcode-coding-interview-so. SinCatGit closed this as completed in 51e3553 on Oct 7, 2020 Count Number of Pairs With Absolute Difference K, 2007. Find Smallest Letter Greater Than Target, 747.