Suppose we have given a String in which some ranges as specified and we have to place the numbers which is between the given range in the specified place as given in the example:
Input : string x = "1-5, 8, 11-14, 18, 20, 26-29" Output : string y = "1, 2, 3, 4, 5, 8, 11, 12, 13, 14, 18, 20, 26, 27, 28, 29"
Approach: To solve the above problem, we can follow the below approach:
- First we have to split the String into String array. We have to split the String where we found – symbol.
- Now we have String array with the elements. Now we just go to the first index last element i.e. 1 and preceding index first element of the String array i.e. 5 .
- After that by the help of for loop we can add the numbers which is between 1 and 5 and store it in the String variable.
- The above process continue til the length of String array.
NOTE: By the help of Collections and various utility methods we can solve the problem easily but Collections concept is not good option performance wise. If we go through Collections, performance is reduced and time complexity is also increased.There in the below program we explicitly define our own split method and logic.
1, 2, 3, 4, 5, 8, 11, 12, 13, 14, 18, 20, 26, 27, 28, 29
Simpler Implementation :
1 2 3 4 5 8 11 12 13 14 18 20 26 27 28 29
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Expand the string according to the given conditions
- String Range Queries to find the number of subsets equal to a given String
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Program to print all palindromes in a given range
- Number of times the given string occurs in the array in the range [l, r]
- Reverse the given string in the range [L, R]
- Queries for Nth smallest character for a given range in a string
- Perform range sum queries on string as per given condition
- Queries to flip characters of a binary string in given range
- Java Program to Determine the Unicode Code Point at Given Index in String
- Queries to count frequencies of a given character in a given range of indices
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Restore original String from given Encrypted String by the given operations
- String generated by typing given string in a keyboard having the button of given character faulty
- Arrange a binary string to get maximum value within a range of indices
- Lexicographically largest string formed from the characters in range L and R
- Queries to find total number of duplicate character in range L to R in the string S
- String Range Queries to count number of distinct characters with updates
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Java Program to get a character from a String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rahulkumarverma0507