0

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • 2
    Do not use the `dsa` tag. Your question has nothing to do with `Digital Signature Algorithm`. – PaulMcKenzie Aug 10 '23 at 22:21
  • *but it's not giving the expected output.* -- What debugging have you done? [What is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). If you wrote the code yourself, you had a plan (on paper). You then implemented the plan in Java. The next step is to see where the program goes against your plan by debugging the program. You will either discover the bug in your code, or you will discover that the plan was flawed and start over with a new plan. – PaulMcKenzie Aug 10 '23 at 22:31
  • 1
    Shabbir, your question was completely unreadable because that 'ask a question' textbox isn't raw text - stars and dashes and the like are formatting primitives. I had to stick everything relevant in backticks to indicate it was 'code' / raw things not to be 'parsed' for format strings. When asking questions on SO, reread your question after posting. Help us help you. – rzwitserloot Aug 10 '23 at 22:39
  • @rzwitserloot Thank you, I was looking at the question and wondering where did the stars come from. – Shovel_Knight Aug 10 '23 at 22:48
  • 1
    Gave it a quick scan - Shabbir, this is just 'help me with my homework' / 'I do not know how to debug code, please do it for me'. Certainly fair questions. However, SO is not meant for such questions. I'm not aware of other sites that are (other than defunct deserts). They probably are deserts because there are __a lot__ of people learning first-steps java and asking low-effort questions, and not nearly enough folks to try and answer it. Perhaps ask your teacher! Voting to close. – rzwitserloot Aug 10 '23 at 23:21

0 Answers0