SORTED STRING INDEX DIFFERENCE

 Consider a non-empty input string, instr containing at least two characters which is a combination of lowercase alphabets and digits. Identify and print the output using the below logic:

  • Separate the alphabets and digits and sort the alphabets in lexicographical ascending order, sortedalpha, and the digits in ascending order, sorteddigit
  • Identify the sort difference for sortedalpha and the sorteddigit
  • Sort difference for a sorted string is the absolute difference between the index of the first occurrence of the smallest element and the index of the first occurrence of the largest element in the original unsorted string, instr
  • Form a string by concatenating sortedalpha followed by sort difference of sortedalpha followed by:"(colon) followed by sum of the digits in instr and the sort difference of sorteddigit and assign it to outstr
  • If the instr consists of only alphabets or only digits, print -1

Note: The lexicographical ascending order is as follows: a,b,c,......z

Input Format

Read the instr from the standard input stream

Constraints

Read the instr from the standard input stream

Output Format

Print outstr or -1 accordingly to the standard output stream

Sample Input 0

gt4r22w7e

Sample Output 0

egrtw2:153

Explanation 0

For the given instr, the alphabets in instr in lexicographical ascending order, Sortedalpha, is "egrtw" and the digits in ascending order, sorteddigit is "2247". The first occurrence of smallest alphabet ‘e’ in instr is present at index 8 and the first occurrence of largest alphabet 'w' in instr is present at index 6 in instr. The absolute difference of these indices is 2. Hence sort difference of sortedalpha is 2 The first occurrence of the smallest digit 2 in instr is present at index 4 and the first occurrence of largest digit 7 in instr is present at index 7. The absolute difference of these indices is 3. Hence the sort difference of sorteddigit is 3. The sum of these digits is 15. Concatenation in the required format results in "egrtw2:153" as the output

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class SortedNumAl {
public static char[] SortStr(String inStr){

char[] inStrArray = inStr.toCharArray();

char temp;

int i = 0;
while (i < inStrArray.length){
int j = i+1;
while (j < inStrArray.length){
if(inStrArray[j] < inStrArray[i]){
temp = inStrArray[i];
inStrArray[i] = inStrArray[j];
inStrArray[j] = temp;
}
j+=1;
}
i+=1;
}

return inStrArray;

}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();


String letters = "";
String numbers = "";
for (int i = 0; i < inputString.length(); i++) {
String tempStr = inputString.charAt(i)+"";
if(tempStr.matches("^[a-zA-Z]+$")){
letters = letters+tempStr;
}else{
numbers = numbers+tempStr;
}
}

if(numbers==""){
System.out.println(-1);
System.exit(0);
}

if(letters==""){
System.out.println(-1);
System.exit(0);
}


char[] letterSorted = SortStr(letters);
char[] numbersSorted = SortStr(numbers);


// Sort difference for Letters
int sortDiffLetter = inputString.indexOf(letterSorted[0]) -
inputString.indexOf(letterSorted[letterSorted.length-1]);
sortDiffLetter = Math.abs(sortDiffLetter);

int sortDiffNumber = inputString.indexOf(numbersSorted[0]) -
inputString.indexOf(numbersSorted[numbersSorted.length-1]);
sortDiffNumber = Math.abs(sortDiffNumber);

int sum = 0;

for (int i = 0; i < numbersSorted.length; i++) {
sum += Integer.parseInt(numbersSorted[i]+"");
}
System.out.println(new String(letterSorted)+sortDiffLetter+":"+sum+""+sortDiffNumber);
}
}


Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER