Monday, March 27, 2023
HomeSoftware EngineeringHow you can get the Numericals of a String in Java

How you can get the Numericals of a String in Java


You’re given an enter string.

For every image within the string if it’s the primary character incidence, substitute it with a ‘1’, else substitute it with the quantity of occasions you’ve already seen it.

Examples:

enter   =  "Hi there, World!"
consequence  =  "1112111121311"

enter   =  "aaaaaaaaaaaa"
consequence  =  "123456789101112"

There is perhaps some non-ascii characters within the string.

Notice: there shall be no int area overflow (character occurrences shall be lower than 2 billion).

The answer in Java code

Choice 1:

import java.util.*;

public class StringNumericals {
    public static String numericals(String s) {
      StringBuilder ret = new StringBuilder();
      Map<String,Integer> map = new HashMap<>();
      for (String c : s.cut up("")) {
        map.put(c, 1 + map.getOrDefault(c,0));
        ret.append(map.get(c));
      }
      return ret.toString();
    }
}

Choice 2:

public class StringNumericals {
    public static String numericals(String s) {
        StringBuilder output = new StringBuilder();
        int readCharacters[] = new int[2024];
        int index;
            for (int i=0; i<s.size(); i++){
                index=(int)s.charAt(i);
                readCharacters[index]++;
                output.append(readCharacters[index]);
            }
        return output.toString();
    }
}

Choice 3:

public class StringNumericals {
    public static String numericals(String s) {
        int[] f = new int[1032];
        return s.chars()
            .map(i -> ++f[i])
            .gather(StringBuilder::new, StringBuilder::append, StringBuilder::append)
            .toString();
    }
}

Check instances to validate our resolution

import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.*;
public class SolutionTest {
    @Check
    public void basicTest0() {
        assertEquals("123456789101112", StringNumericals.numericals("aaaaaaaaaaaa"));
    }
    @Check
    public void basicTest1() {
        assertEquals("1112111121311", StringNumericals.numericals("Hi there, World!"));
    }
    @Check
    public void basicTest3() {
        assertEquals("11121122342", StringNumericals.numericals("hi there hi there"));
    }
    
    @Check
    public void fixedTest1() {
        assertEquals("12345", StringNumericals.numericals("11111"));
    }
    @Check
    public void fixedTest2() {
        assertEquals("1111112121111111113212311414121151151262267232231",
        StringNumericals.numericals("hope you 123456789 anticipated numbers within the string"));
    }
    @Check
    public void fixedTest3() {
        assertEquals("11111112221221132112411115312263237221234482193101343525441123124155131",
        StringNumericals.numericals("On this string, I am going to be sure the quantities of a personality go over 9"));
    }
    public static String reply(String s) {
      StringBuilder ret = new StringBuilder();
      Map<String,Integer> map = new HashMap<>();
      for (String c : s.cut up("")) {
        map.put(c, 1 + map.getOrDefault(c,0));
        ret.append(map.get(c));
      }
      return ret.toString();
    }
    @Check
    public void randomTests() {
        for (int i = 0; i < 100; i++) {
            int N = (int)(Math.random()*10 + 200);
            char[] r = new char[N];
            for (int j = 0; j < N; j++) {
                char c = (char)(Math.random()*1000 + 32);
                r[j] = c;
            }
            String s = String.valueOf(r);
            String anticipated = reply(s), precise = StringNumericals.numericals(s);
            assertEquals(anticipated,precise);
        }
    }
    @Check
    public void randomTestsBiggerStrings() {
        for (int i = 0; i < 100; i++) {
            int N = (int)(Math.random()*100 + 100000);
            char[] r = new char[N];
            for (int j = 0; j < N; j++) {
                char c = (char)(Math.random()*200 + 32);
                r[j] = c;
            }
            String s = String.valueOf(r);
            String anticipated = reply(s), precise = StringNumericals.numericals(s);
            assertEquals(anticipated,precise);
        }
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments