Description:
problem name - String hasher
problem statement:
you are given a string and a number(sub-string length). the string consists of *
,-
, and +
characters, and the string's length will be the multiple of the given number. Write a function to hash the string using the below hashing algorithm:
- split the given string into multiple sub-strings of a given length string from left
- calculate the weight of each sub-string. the number of times a unique sub-string value appears after splitting is defined as the weight of that sub-string
-sort the unique sub-string values by weight in descending order. if 2 or more sub-strings have the same weight, then sort them by character order.
*
>-
>+
-create the hash by concatenating the sorted list of unique sub-strings and return the hash.
Example 1:
**input**: -+*++-*++-
sub-string length:2
**output**: *++--+
Explanation:
sub-string with length 2:
-+*++-*++-
unique sub-strings with weight
- -+ : 1
- *+ : 2
- +- : 2
- while sorting sub-strings by weight,
*+
and+-
sub-strings have same weight. i.e 2. so, we sort these sub-strings by character order resulting in *+ coming before +-.so,the final order is*++--+
- create the hash by concatenating the unique sub-string:
*++--+
Sample Input
-+*++-*++-
2
sample output
*++--+
Java pseudo code to be built
import java.io.* ;
import java.util.* ;
import java.text.* ;
import java.math.* ;
import java.util.regrex.* ;
class Main{
public static void main ( String [] args) throws Exception {
StringBuilder inputData=new StringBuilder() ;
String thisLine=null ;
BufferReader br= new BufferReader (new InputStreamReader(System.in));
while((thisLine=br.readLine())!=null){
inputData.append(thisLine + "/n");
}
System.out.println(codeHere(inputData));
}
public static string codeHere(StringBuilder inputData){
// use this function to write your solution;
return inputData.toString();
}
my code :
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
StringBuilder inputData = new StringBuilder();
String thisLine = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while ((thisLine = br.readLine()) != null) {
inputData.append(thisLine + "\n");
}
System.out.println(codeHere(inputData));
}
public static String codeHere(StringBuilder inputData) {
String[] lines = inputData.toString().split("\n");
String inputString = lines[0].trim();
int subStringLength = Integer.parseInt(lines[1].trim());
return hashString(inputString, subStringLength);
}
public static String hashString(String inputString, int subStringLength) {
Map<String, Integer> substringCount = new HashMap<>();
for (int i = 0; i <= inputString.length() - subStringLength; i++) {
String substring = inputString.substring(i, i + subStringLength);
substringCount.put(substring, substringCount.getOrDefault(substring, 0) + 1);
}
List<String> uniqueSubstrings = new ArrayList<>(substringCount.keySet());
uniqueSubstrings.sort((a, b) -> {
int weightComparison = substringCount.get(b) - substringCount.get(a);
if (weightComparison == 0) {
return a.compareTo(b);
}
return weightComparison;
});
StringBuilder hash = new StringBuilder();
for (String substring : uniqueSubstrings) {
hash.append(substring);
}
return hash.toString();
}
}
but it's not giving the expected output.
for input:
-+*++-*++-
2
output
*++++-+*-*-+
expected output
*++--+
Please Give easiest method if possible.