D
D
Dima Zherebko2015-12-07 18:00:13
Java
Dima Zherebko, 2015-12-07 18:00:13

How to solve a problem without arrays?

The essence of the problem: there is a string, you need to find the number of characters that are most common and output in this type
if the original string is hhyhddrdffrfaasxf
h-3
d-3
f-3
With arrays, the problem is very simple. How to solve this problem without using arrays and collections.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Optimus, 2015-12-07
Pyan @marrk2

Well, in php it is possible through str_replace by using the last parameter count (count of replacements made), in java there is an analogue or not, I don’t know ...

�
âš¡ Kotobotov âš¡, 2015-12-07
@angrySCV

Well, since hashes and tables can't be used, you're probably left with just graph data structures.

S
SilentFl, 2015-12-08
@SilentFl

just on the forehead?

class G_tost {
  public static int getCount(String s, char ch) {
    int res = 0;
    for (int i = 0; i < s.length(); i++) {
      res += s.charAt(i) == ch ? 1 : 0;
    }
    return res;
  }

  public static void main(String[] args) {
    String data = "hhyhddrdffrfaasxf";
    int ans = 0;
    char ch  = data.charAt(0);
    for (int i = 0; i < data.length(); i++) {
      int l = getCount(data, data.charAt(i));
      if (ans < l) {
        ch = data.charAt(i);
        ans = l;
      }
    }
    System.out.format("Char: %c, count: %d\n", ch, ans);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question