Find all unique substrings in a string java Note that "bca" and "cab" are also correct answers. Write a Java program to determine the length of the longest substring with no repeating characters using a sliding window. Additionally, we need to check that the length is not greater than the ASCI set size (in this case we use 256 for extended ASCI). util. If the given substring is not present in the Set S, insert it updates the maximum count, and remove it from the Set, because the same character cannot be reused. Mar 25, 2016 · This was a question asked in a recent programming interview. May 15, 2015 · I am trying to find all possible distinct substrings of a string. Write a Java program to extract the longest substring without duplicates and then print its characters in sorted order. * For example, calling countUniqueChars (s) if s = "LEETCODE" then "L", "T", "C", "O", "D" are the unique characters since they appear only once in s, therefore countUniqueChars (s) = 5. Now, for every distinct sub-string, count the distinct characters in it (again set can be used to do so). Intuitions, example walk through, and complexity analysis. For example, the longest substring of unique letters in “CODINGISAWESOME” is “NGISAWE”. The outer loop runs n n times, where n n is the length of the input string. Examples : (a) "" is a String in java with 0 character (b) "d" is a String in java with 1 character (c) "This is a sentence. Examples : Input : str1 = "HELLO" str2 = "WORLD" Output : YES Explanation : The substrings "O" and "L" are common to both str1 and str2 Input : str1 = "HI" str2 = "ALL" Output : NO Explanation : Because str1 and str2 have no common substrings A basic approach runs in O (n^2), where we This program creates and prints all substrings possible with a given string. Examples: Input: s = "geeksforgeeks" Output: 7 Explanation: The longest substrings without repeating characters are "eksforg” and "ksforge", with lengths of 7. If there are two or more equal substrings at different positions in S, we consider them different. Do you mean the part that prints results? This cannot be O(n), as results (printed out as strings) are of size O(n^2) - consider string "aaaaaaaaaa", results here. – Brute force method – Simplest approach to generate substrings having all unique characters from a given string and return the maximum length Sliding window method Aug 29, 2025 · Given a string s, find the total number of palindromic substrings of length greater than or equal to 2 present in the string. Jul 15, 2025 · The simplest approach is to generate all possible substrings from the given string and check for each substring whether it contains all distinct characters or not. This method is most useful when you deal with text manipulation, parsing, or data extraction. Jul 23, 2025 · For string “ababa”, lcp array is [1, 3, 0, 2, 0] After constructing both arrays, we calculate total number of distinct substring by keeping this fact in mind : If we look through the prefixes of each suffix of a string, we cover all substrings of that string. I know it's not correct but I feel like I am going about it the right way. Apr 2, 2022 · Find all Palindrome Substrings Given a string, find all non-single letter substrings that are palindromes. Below is the implementation of the above approach: Printing them touches all of them. Count Unique Characters of All Substrings of a Given String - Let's define a function countUniqueChars (s) that returns the number of unique characters in s. Jul 23, 2025 · Note: The above Code not works if we have all characters. Naive Approach 3. Jan 8, 2024 · Learn how to solve the "needle in a haystack" problem by using the indexOf method to find all occurrences of a word in a larger text string. Examples : Input : abcd10jk Output : true Input : hutg9mnd!nk9 Output : false Approach 1 - Brute Force technique: Run 2 loops with variable i and j. Below is the implementation of the above approach: Definition and Usage The substring() method returns a substring from the string. Final Thoughts Where can you use this? Once you understand the basic logic, you can use it to solve many string related problems. Use a for loop to iterate through the characters of the original string, starting from the first character. Feb 3, 2020 · All you need is the count? If I'm figuring this right: A given string length n contains n substrings of length 1, n-1 substrings of length 2, n-2 substrings of length 3, to 1 substring of length n. The approach I am considering is for each string look at all other strings and find the common characters and where differing characters are, trying to find sets of strings that have the most in common, but I fear that this is not very efficient and may give false positives. Compare str [i] and str [j]. Retrieve maximum. Given a random string S and another string T with unique elements, find the minimum consecutive sub-string of S such that it contains a Jan 8, 2024 · Learn several algorithms for finding all palindromes within a given string in Java. A String in java can be of 0 or more characters. find total length of each unique palindrome). For every substring, check whether each particular character has previously occurred or not. Using indexOf(char c) The indexOf() searchesfor the first occurrence of a Jul 23, 2025 · A substring is a contiguous part of a string, i. Mar 16, 2012 · Given a string S of length N find longest substring without repeating characters. Main Idea The main idea in this approach is to generate all possible subsequences of the given string. In this article, we will explore essential methods like indexOf (), contains (), and startsWith () to search characters and substrings within strings in Java. There could be various other (simpler) ways to achieve this The basic idea is simple: for each starting index, find the least ending index such that the substring contains all of the necessary letters. Thus, all duplicate substrings will be discarded as a hash set always containing the unique elements. println( index = word. You are given a string s and an integer k. The palindromic substrings are 'a', 'abba', 'b', 'bb', 'b', 'bab', 'a', 'b'. To find all anagrams of p in string s, we need to check if substrings of s have the same character frequencies as p. Split a String Into the Max Number of Unique Substrings in Python, Java, C++ and more. This means that no character should be repeated within a single substring. Iterate over the word array using for loop. This tutorial will explore the concept of finding repeated substrings in Java, providing practical examples and insightful explanations. It takes user input example: "abbaalla" it loops through creating a substring that changes as the loop progresses. To do so, create an array of string res [] to store the substrings of string s and an empty string cur to store the current string. Jul 23, 2025 · The idea to count the occurrence of the string in the string and print if count equals one Extract words from string using split () method and store them in an array. Below is the implementation of the above approach: Jul 21, 2024 · The practical ways of using the useful substring functionality in Java - from simple examples to more advanced scenarios. First of all you have to collect all unique palindromes and count how many time each of them occures in the string. To make it versatile we have to use long Double data type. The sum of this count for all the distinct sub-strings is the final Learn how to check if a string in Java contains all unique characters using various methods with practical code examples. Examples: Input: S = "abaaba" Output: 2 Explanation: The smallest substring in the string S, whose occurrence is exactly 1 is "aa" . To do so, create a HashSet to store the subsequences, and call a recursive function for all the indices of the string, starting from the 0th index. If they become equal at any point, return false. Additionally, all the methods discussed here are case-insensitive. In this video, you'll learn how to count occurrences of a substring within a string in Java using three different approaches: a traditional loop, Java 8 Streams, and a HashMap for substring Mar 3, 2024 · It starts with an empty set substr_set, where it stores each substring to ensure that all substrings are unique. Palindromic Substrings in Python, Java, C++ and more. Method 1: Brute Force Approach This method Jun 23, 2016 · unless what you want is all combinations of the characters in the string in the forward order. I'm trying to find all occurrences of a substring in a string in Java. If the second occurrence is found increment count and set the word to "" If the count of the current word is equal Problem Description Given a string s, you need to find and return the total number of distinct (unique) substrings that can be formed from it. A palindrome is a word, phrase, number, or other sequence of characters that reads the Jun 2, 2023 · If we find that the character already exists in the structure, we know the string does not contain only unique characters. ". out. Scanner; public class Solution { public static void main (String [] args) { Jul 24, 2019 · The task is to obtain a unique list of substrings in python. Finding a substring within a string is a common task in Java. Sep 11, 2022 · Given a string you need to print the size of the longest possible substring that has exactly K unique characters. Oct 15, 2025 · Find all palindrome substrings of a string with efficient algorithms. Iterate over the map and miltiply palindrome length and it's occurence (i. Jul 23, 2025 · Efficient String manipulation is very important in Java programming especially when working with text-based data. FileName Every substring is composed with only unique letters. If the length of string is N, then there will be N* (N+1)/2 possible substrings. Examples: Input: s = "timetopractice", p = "toc" Output: toprac Explanation: "toprac" is the smallest substring in which Jul 23, 2025 · Iterate over the characters of the string str and for each i and find the substring up to that index. Examples: Input : hellolle Output : 13 [h, e, l, ll, l, o, lol, lloll, ellolle, l, ll, l, e] Explanation: ellolle ll Feb 11, 2025 · The idea behind this code is to find all substrings of the input string that contain all the vowels ('a', 'e', 'i', 'o', 'u'). Time complexity of this solution would be O (n^3). Jun 28, 2012 · How do you find the shortest substring of each element in an array of strings where the substring is NOT contained within any of the other elements, ignoring case? May 21, 2025 · Write a Java program to find the longest substring in a string that contains all unique characters. In general, for an string of size n, there are n* (n+1)/2 non-empty substrings. example: checks palindrome "a" The algorithm efficiently identifies all special substrings by maintaining a sliding window of unique characters and counting valid substrings ending at each position, avoiding the need to check every possible substring individually. If no such substring exists, return "". By working through the task of detecting and recording the starting indices of substrings, learners will gain experience with Java's ArrayList, loops, and String methods. Aug 3, 2022 · Approach: Take the substrings from the given string. Given a string s, return the number of unique non-empty substrings of s are present in base. A substring is a contiguous sequence of characters within a string. Print and display on the console. indexOf(guess) ); I would like to know how to get all the indexes of Jan 8, 2024 · Learn two ways of checking if a String consists only of its substrings in Java. Let's dive deep into the topic. e. Create an array arr of size 26 with 0. Note : Our output can have same character. Can you solve this real interview question? Find Unique Binary String - Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. You have to check if the two strings share a common substring. The task is to find all unique substrings containing only vowels. For example, given the input strings eatsleepnightxyz and eatsleepabcxyz, the results should be: eatsleep (due to eatsleep nightxyz eatsleep abcxyz) In-depth solution and explanation for LeetCode 647. You've counted all unique substrings. What is a substring? According to Wikipedia, The contiguous sequence of characters within a String is called substring. Input: s = "aaa" Output: 1 Explanation: The longest substring without repeating characters is "a" Input: s Dec 30, 2018 · You just have to refactor your code a little bit. I am wondering is there a better way of coding this? I want to use recursion. This guide will cover different ways to find a substring, including using the indexOf method, the contains method, and regular expressions. The naive approach would be to check every possible substring of length len(p) in s, but this would involve repeatedly counting characters for overlapping windows. Given a string S with only uppercases, calculate the sum of UNIQ(substring) over all non-empty substrings of S. " is a string with 19 characters. Jul 23, 2025 · Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more. Feb 14, 2012 · Possible Duplicate: How to see if a substring exists inside another string in Java 1. I have implemented a method which will tell you all the substrings of string but i need a help figuring out how to not count one which is already counted once as substring because assignment is to find distinct one. Sep 9, 2025 · Given a string and a positive integer `k`, find all distinct substrings of any length containing exactly `k` distinct characters. But how would you "Generate all unique substrings for given string" with a suffix array/longest common prefix array? Jul 12, 2025 · Approach: The idea is to iterate over all the substrings. The inner loop runs n i n − i times for each iteration of the outer loop. Here, we discuss the optimal method to achieve this, taking into consideration both time and space complexities. In each iteration of the loop, use slicing to extract a substring of length 'n' from the original string, starting from the current character. where we can incorporate all frequency of every character. May 31, 2019 · Given a string S of length N consisting of lower-case English alphabets and an integer 'l', find the number of distinct substrings of length 'l' of the given string. It produces a Stream of MatchResult objects which correspond to captured substrings, and the only thing needed is to apply Stream. In this blog post, we'll explore the various ways to find substrings in Java, including built - in methods, common practices, and best practices. So in this core java programming tutorial we wrote a program how to find out all possible unique subString present in a given string in java . Dec 7, 2022 · For finding a total number of substrings that are in alphabetical order traverse the given string and compare two neighboring characters, if they are in alphabetic order increment the result and then find the next character in the string which is not in alphabetic order to its former character. For example: Finding the longest palindromic substring. Aug 16, 2023 · Java Unique String Check: Verify if a Java string contains only unique characters using techniques: HashSet, Sorting, Brute-Force, and more. The generated substrings can be put in a hash set. The approach is to iterate over all possible starting points of substrings in the given string. One moment, pleasePlease wait while your request is being verified Apr 5, 2022 · Given string str of size N consisting of uppercase and lowercase English letters. ll and ll are considered as two sub-strings, not one. Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10 Example 2: Input: s = "ABA" Output: 8 Explanation: The same as example 1, except countUniqueChars ("ABA") = 1. count() to obtain the number of elements in the stream. Whether a substring contains all unique characters or not can be checked in linear time by scanning it from left to right and keeping a map of visited characters. There will be n* (n+1)/2 substrings. Jun 13, 2013 · 4 I want an algorithm to find the longest substring of characters in a given string containing no repeating characters. If any character doesn't match, we can say the string is not palindromic and move ahead. 2. In the world of programming, handling strings efficiently is a vital skill. *; public class GFG { In-depth solution and explanation for LeetCode 467. Great. In this article, we will generate all the Finding the most repeated substring across a collection of strings is a useful task in text analysis, data compression, and pattern recognition. Examples: Input: s = "abacaba" Output: 4 Explanation: Two possible partitions are ("a Given a list of strings, sort their substrings in lexicographic order and remove duplicates. A substring is palindromic if it reads the same forwards and backwards. We will explain the procedure for above example, Number of Distinct Substrings. Example: Input: "stackoverflow" Output: "stackoverfl" If there are two such candidates, return first from left Mar 10, 2024 · Problem Formulation: The task is to identify all unique substrings within a provided string that read the same backwards as forwards, known as palindromes. For example, in the string "abc", the substrings of length 2 are "ab" and "bc". The trick is that the least ending index increases over the course of the function, so with a little data structure support, we consider each character at most twice. Now check whether the substring is Palindrome or not. * The substring size must be between minSize and maxSize inclusive. If the substring is Palindrome increment the count by 1 else if not count is not incremented. It systematically extracts each substring and prints it by the use of two nested loops. Is that what you're looking for? Find All Anagrams in a String - Given two strings s and p, return an array of all the start indices of p's anagrams in s. Jan 27, 2023 · You are given two strings str1 and str2. Finding all possible substrings in a string is a common task that can be approached in several ways. Unique Substrings in Wraparound String in Python, Java, C++ and more. Find All Substrings of a String in Java with Example In this tutorial, I will be sharing what is substring, programs for finding substrings of a String in java with examples. Return the count as it represents the number of substrings. Approach: Simple approach in which counting number of alphabets present in string using array. If the end argument is not specified then the substring will end at the end of the string. If so, then increase the count of required substrings. Searching for a Character in a String 1. Think of it as using scissors to cut out exactly the piece of text you need. Hovewer, array itself is O(n) size and contains all data neccessary to print out the palindromes. Apr 1, 2010 · Okay. I need to find all the palindromes in a string. The problem differs from the problem of finding the possible palindromic subsequence. Example 1: Input: s = "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c". Example 1: Input: s = "cbaebabacd", p = "abc" Output: [0,6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". Oct 3, 2025 · Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more. This process involves identifying the frequency of each character in the input string and displaying it in a readable format. Jul 23, 2025 · Given a string, determine if the string has all unique characters. A substring is a contiguous sequence of characters within the string. Oct 27, 2021 · Complexity I guess the worst case time here could be (2^n)-1 if we don't use a set to check for unique values (where n is length of the given string). Unlike subsequences, substrings are required to occupy consecutive positions within the original string. Apr 9, 2025 · In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. , a string inside another string. Oct 1, 2024 · Naive Recursive Approach A very naive approach would be to generate all possible subsequences of the string and check each one to see if it's a palindrome. Jul 11, 2025 · Given a string, the task is to find all the palindromic sub-strings from the given string. You can find the number of occurrences of a substring in a string using Java 9 method Matcher. The size of the hash set is our answer. See full list on tutorialkart. 4 How would I search for a string in another string? This is an example of what I'm talking about: String Learn how to print unique characters from a string in Java with step-by-step examples and best practices. Count Unique Characters of All Substrings of a Given String in Python, Java, C++ and more. While the brute-force method is straightforward, it is not always efficient. Your task is to find how many substrings of length k exist in the string s where all characters in the substring are unique (no repeated characters). The basic idea is simple: for each starting index, find the least ending index such that the substring contains all of the necessary letters. However, do not include substrings that are contained within longer common substrings. We will understand the time complexity of the program. Aug 5, 2024 · Given a string of length N, find the length of the smallest sub-string consisting of maximum distinct characters. This is my current code: import java. Apr 11, 2025 · In Java, the substring () method of the String class returns a substring from the given string. You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. This lesson focuses on string manipulation in Java, specifically on how to find all occurrences of a substring within a larger string. We find the set with the maximum length using Python’s max function with the key parameter set to len, which gives us the desired output. I can think of an O (n*n) algorithm which considers all the substrings of a given string and calculates the number of non-repeating characters. May 20, 2025 · The substring() method is a built-in function of Java‘s String class that allows you to extract a portion of a string based on specified indices. A string is a palindrome when it reads the same backward as forward. Examples: Input: str = "GeeksforGeeks" Output: "ee" , "e", "o" Explanation: There are multiple occurrences of some of the substrings like "ee" but these are the only unique substrings. Aug 4, 2025 · Given a string s having lowercase characters, find the length of the longest substring without repeating characters. In this tutorial, we will implement an important data structure question known as finding the longest substring without repeating characters. Example The Jan 8, 2024 · In this tutorial, compare ways to find the longest substring of unique letters using Java. Jan 8, 2024 · In this tutorial, we’ll learn various techniques to check if all the characters present in a string, including non-ASCII characters, are unique. Sep 17, 2025 · Given a string, find all possible palindromic substrings in it. Time complexity: O (N3) Auxiliary Space: O (1) Efficient Approach: The problem can be solved in linear time using Two Pointer Technique, with the help Problem Description You are given a string s and three integer parameters: maxLetters, minSize, and maxSize. Jul 11, 2025 · Number of substrings of length one is n (We can choose any of the n characters) Number of substrings of length two is n-1 (We can choose any of the n-1 pairs formed by adjacent) All we need to do is to generate all of the substrings of the given string using nested for-loops and the substring () method. Apr 13, 2025 · The idea is to generate all possible subsequences of the given string str, and store them in a HashSet to find the count of distinct subsequences. One common problem is identifying repeated substrings within a given string. Return the maximum count. This problem is a common Amaz Jul 23, 2025 · Given string 's', the task is to divide a given string s into multiple substrings, with each substring containing only unique characters. Can you solve this real interview question? Longest Substring Without Repeating Characters - Given a string s, find the length of the longest substring without duplicate characters. Dec 5, 2022 · Hello. Word has a constructor that takes a string argument and one method getSubstrings which returns a String containing all substring of word, sorted by length. Palindromic Substrings - Given a string s, return the number of palindromic substrings in it. Length of this substring is 2. This video explains a very important interview problem which is to Count Unique Characters of All Substrings of a Given String. Use another loop to find the occurrence of the current word the array. com Feb 14, 2025 · The idea is to recursively generate all possible substrings of the given string s. For each subsequence, check if it's a palindrome. Then for each character c, and each second character d, find all locations where the string "cd" can be found - we do this preferably first for characters c that are rare. I am currently using the breakup of the problem into 2 parts: obtain a list of all the substrings, followed by obtaining unique substri Mar 2, 2023 · Approach: Firstly generate all the substring of length L and then by using set we can insert unique sub-string till the length L and then print the result. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. The find_palindromes_bruteforce function uses nested loops to generate all possible substrings of the input string and checks each substring for being a palindrome using the is_palindrome function. Algorithm : Iterate over string length: Mar 1, 2011 · Here is another spoj problem that asks how to find the number of distinct subsequences of a string ? For example, Input AAA ABCDEFG CODECRAFT Output 4 128 496 How can Nov 26, 2016 · I have the following function to get all of the substrings from a string in JavaScript. Jul 1, 2025 · Given a string s and a non negative integer k, find the length of the longest substring that contains exactly k distinct characters. 1. For each starting point, we explore all possible ending points to form substrings. results() with a single line of code. Take a string s and convert it into lowercase if not. Example: import java. If no such substring exists, return -1. Jul 3, 2013 · I created class Word. Jul 23, 2025 · Given a String, the task it to split the String into a number of substrings. Mastering string manipulation not only enhances your coding skills but also improves your problem-solving Mar 31, 2023 · Initialize a variable 'n' to the desired length of the substrings. Feels like the number of substrings should be (n^2 + n) / 2. It is essentially a portion of the string from the start index and end index. Jul 23, 2025 · Method 1 (Simple : O (n3)): We can consider all substrings one by one and check for each substring whether it contains all unique characters or not. The goal is to find the minimum number of such substrings required to satisfy this condition. For example, if maxLetters = 2, then substrings like "aa", "ab", "bb" are valid Jun 26, 2022 · For a school assignment, I have to implement a method that will return the longest repeated substring from a string. The function returns the count of distinct substrings, which is simply the size of the set. For example, given the input string “aabbbaa”, the desired output should be a set of distinct palindromes such as {“a”, “aa”, “b”, “bb”, “bbb”, “abbba”, “aabbbaa”}. Count all subsequences that are palindromes. You may return the answer in any order. Maximum Number of Occurrences of a Substring - Given a string s, return the maximum number of occurrences of any substring under the following rules: * The number of unique characters in the substring must be less than or equal to maxLetters. Jul 12, 2025 · Input: str = "AAAB" Output: 10 Approach: Take all possible sub-strings of the given string and use a set to check whether the current sub-string has been processed before. Unique Substrings in Wraparound String - We define the string base to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so base will look like this: * "zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd. We repeat for three, four, five character strings and so on. 1. A substring is defined as a contiguous sequence of characters within the string, and a palindrome reads the Learn how to find palindrome substrings in Java with this step-by-step tutorial, complete with code snippets and best practices. We will match the characters of the sub-string from the start and end to check whether the string is a palindrome. If multiple substrings of the same length are found, return the one with the smallest starting index. In the end return this count as count of all unique substrings with non-repeating characters. public class main { p Jul 23, 2025 · Given a string S consisting of N lowercase alphabets, the task is to find the length of the smallest substring in S whose occurrence is exactly 1. Again we might be lucky and find a two letter string occurring only once. Mar 4, 2024 · This snippet defines a function splitString that takes a string and uses the backtrack helper function to generate all possible combinations of unique substrings, which are then stored in the best set. Using for Loop In this approach, we will use the for loop to get all sub-strings of the given string. io. For example: searching "ababsdfasdfhelloasdf" for "asdf" would return [8,17] since there are 2 "asdf"'s, one at position 8 and one at 17. Aug 17, 2013 · The following code in Java uses recursion to create all possible substrings from a string. Jan 15, 2016 · The problem statement is as follows: Write a program to find the common substrings between the two given strings. For example, if th Aug 13, 2025 · In this blog, we will learn about Java Program to Find All Palindromic Sub-Strings of a String. Dec 13, 2023 · A substring is a continuous sequence of characters within a string. In this article, you will learn how to implement an algorithm to count the number of distinct dubstrings in a String, with an example in Java. Sep 13, 2024 · Given a string s, you need to find the length of the longest substring that contains no repeating characters. If you wanted to ask, say, "what is the longest substring that appears at least twice" or "which substring of more than k characters occurs most frequently", then you can do so without enumerating all substrings (with a suffix tree). This Educative guide explains expand-around-center and DP tradeoffs, step-by-step examples, edge cases, complexity, and implementations in Python, Java, C++, JavaScript, and Ruby. Mar 18, 2024 · Given a string , let’s get all the distinct subsequences of that string (red squares represent the selected characters): As we can see, there are different subsequences of the given string . However, you must split the substrings such that all of them are unique. In this Java program, we aim to identify the substring that occurs most frequently in all strings of a given array, helping developers practice substring generation and frequency counting. Better than official and forum solutions. If there is no possible substring then prin The following code will print 2 String word = "bannanas"; String guess = "n"; int index; System. The structure used below is a Boolean array. Aug 13, 2024 · Given a string s, your task is to return the number of palindromic substrings in it. For example: If input is “abb” then output should be “a”, “b”,”b”, “ab”, “bb”, “abb” Nov 12, 2025 · Whether you're parsing text, validating user input, or processing data, the ability to extract specific parts of a string is crucial. Steps-by-step approach: Generate all subsequences of the string. Jun 10, 2024 · Learn how to print unique characters from a Java string using Set, Streams, and LinkedHashMap with detailed examples. Introduction The implementation will talk about three different areas i. Examples: Input : "AABBBCBB" Output : 5 Input : "AABBBCBBAC" Output : 3 Explanation : Sub-string -> "BAC" Input : "GEEKSGEEKSFOR" Output : 8 Explanation : Sub-string -> "GEEKSFOR" Method 1 (Brute Force) We can consider all sub-strings one . Your task is to find the maximum number of times any substring appears in s, where the substring must satisfy two conditions: Character limit: The substring must contain at most maxLetters unique characters. Jan 8, 2024 · A quick and practical guide to checking if a String has all unique characters in Java. In-depth solution and explanation for LeetCode 1593. Input: S = "zyzyzyz" Output: 5 Approach: To solve the problem Given a string s, what is the java program to generate a set of all its unique substrings for a given length k ? Example: for str = "abdd" we would get substrs= {"ab" ,"ad" ,"ba" , "bd" ,"da" , "db"} for k = 2 Aug 6, 2025 · Given two strings s and p, the task is to find the smallest substring in s that contains all characters of p, including duplicates. Jan 12, 2021 · In this post, we will see java program to find all substrings of a String. 3. In Set - 1, another approach has been already discussed and that consider only distinct sub-strings but in this equal sub-strings i. Learn how to print unique characters from a string in Java with step-by-step examples and best practices. But I have to do it by using only the Stream API. Every substring is composed with only unique letters. This approach is highly inefficient due to the exponential number of subsequences. Loop the the string s In-depth solution and explanation for LeetCode 828. Therefore, print 2. If there are multiple answers, you may return any of them.