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 provided and depicted in the illustration below as follows for a better understanding.
Illustration:
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:
In order 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 a String[] array with the elements. Now we just go to the first index last element i.e. 1 and the preceding index first element of the String[] array say it be 5.
- After that with the help of for loop, we can add the numbers which are between 1 and 5 and store them in the String variable.
- The above process continues till the length of the string array.
Note:
With the help of Collections and various utility methods we can solve the problem easily but the Collections concept is not a 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.
Implementation: Here we will now be proposing and discussing all three scenarios with help of a clean java program as follows:
Example 1:
Java
public class Solution {
public static void expand(String word)
{
StringBuilder sb = new StringBuilder();
String[] strArr = word.split( ", " );
for ( int i = 0 ; i < strArr.length; i++) {
String[] a = strArr[i].split( "-" );
if (a.length == 2 ) {
int low = Integer.parseInt(a[ 0 ]);
int high
= Integer.parseInt(a[a.length - 1 ]);
while (low <= high) {
sb.append(low + " " );
low++;
}
}
else {
sb.append(strArr[i] + " " );
}
}
System.out.println(sb.toString());
}
public static void main(String args[])
{
String s = "1-5, 8, 11-14, 18, 20, 26-29" ;
expand(s);
}
}
|
Output: 1 2 3 4 5 8 11 12 13 14 18 20 26 27 28 29
Example 2:
Java
public class StringExpand {
static String[] split(String st)
{
int wc = countWords(st);
String w[] = new String[wc];
char [] c = st.toCharArray();
int k = 0 ;
for ( int i = 0 ; i < c.length; i++) {
String s = "" ;
while (i < c.length && c[i] != ' ' ) {
s = s + c[i];
i++;
}
if (s.length() != 0 ) {
w[k] = s;
k++;
}
}
return w;
}
static int countWords(String str)
{
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (i == 0 && str.charAt(i) != ' '
|| str.charAt(i) != ' '
&& str.charAt(i - 1 ) == ' ' ) {
count++;
}
}
return count;
}
public static void expand(String s)
{
String p = s;
String[] arr = p.split( "\\-" );
String k = "" ;
for ( int i = 0 ; i < arr.length; i++) {
if (i != arr.length - 1 ) {
String[] arr1 = arr[i].split( ", " );
String[] arr2 = arr[i + 1 ].split( ", " );
int a = Integer.parseInt(
arr1[arr1.length - 1 ]);
int b = Integer.parseInt(arr2[ 0 ]);
for ( int j = a + 1 ; j < b; j++) {
arr[i] = arr[i] + ", " + j;
}
}
if (k != "" )
k = k + ", " + arr[i];
else
k = k + arr[i];
}
System.out.println(k);
}
public static void main(String[] args)
{
String s = "1-5, 8, 11-14, 18, 20, 26-29" ;
expand(s);
}
}
|
Output: 1, 2, 3, 4, 5, 8, 11, 12, 13, 14, 18, 20, 26, 27, 28, 29
Example 3:
Java
class GFG {
public static String generateStringOn(String input)
{
String[] words = input.split( " " );
String[] ranges = null ;
int number = 0 ;
StringBuffer buffer = new StringBuffer();
for (String word : words) {
word = word.replace( "," , "" );
if (word.contains( "-" )) {
ranges = word.split( "-" );
number = Integer.parseInt(ranges[ 0 ]);
while (number
<= Integer.parseInt(ranges[ 1 ])) {
buffer.append(number + ", " );
number++;
}
}
else {
buffer.append(word + ", " );
}
}
return buffer.toString();
}
public static void main(String[] args)
{
String input = "1-5, 8, 11-14, 18, 20, 26-29" ;
System.out.println(generateStringOn(input));
}
}
|
Output1, 2, 3, 4, 5, 8, 11, 12, 13, 14, 18, 20, 26, 27, 28, 29,