Tuesday, April 23, 2024
HomeSoftware EngineeringTips on how to get the Numericals of a String in Java

Tips on how to get the Numericals of a String in Java


You might be 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   =  "Hiya, World!"
consequence  =  "1112111121311"

enter   =  "aaaaaaaaaaaa"
consequence  =  "123456789101112"

There could be some non-ascii characters within the string.

Notice: there will probably be no int area overflow (character occurrences will probably 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.break 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])
            .acquire(StringBuilder::new, StringBuilder::append, StringBuilder::append)
            .toString();
    }
}

Take a look at circumstances to validate our resolution

import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.*;
public class SolutionTest {
    @Take a look at
    public void basicTest0() {
        assertEquals("123456789101112", StringNumericals.numericals("aaaaaaaaaaaa"));
    }
    @Take a look at
    public void basicTest1() {
        assertEquals("1112111121311", StringNumericals.numericals("Hiya, World!"));
    }
    @Take a look at
    public void basicTest3() {
        assertEquals("11121122342", StringNumericals.numericals("whats up whats up"));
    }
    
    @Take a look at
    public void fixedTest1() {
        assertEquals("12345", StringNumericals.numericals("11111"));
    }
    @Take a look at
    public void fixedTest2() {
        assertEquals("1111112121111111113212311414121151151262267232231",
        StringNumericals.numericals("hope you 123456789 anticipated numbers within the string"));
    }
    @Take a look at
    public void fixedTest3() {
        assertEquals("11111112221221132112411115312263237221234482193101343525441123124155131",
        StringNumericals.numericals("On this string, I will make sure that 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.break up("")) {
        map.put(c, 1 + map.getOrDefault(c,0));
        ret.append(map.get(c));
      }
      return ret.toString();
    }
    @Take a look at
    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);
        }
    }
    @Take a look at
    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