There is a word, it is necessary to break it down by letters and to assign each letter a serial number in the order of the location of this letter in the alphabet. Example

enter image description here

Created a class with two fields of type char and int . Reached before I received a serial number in the alphabet, and how to replace numbers from the alphabet with a sequence number in the word I can not think of. It turns out this list:

 Р А Д О С Т Ь 17 1 5 15 18 19 29 
  • 2
    Give a listing of your code to help you finish it. - Aries
  • There is no code, the brain cannot figure out how to do this without sorting (which I don’t need - Mikhail Ketov
  • And if the word has several identical letters? - Qwertiy
  • @Qwertiy will not work this way, it doesn’t need to be taken into account - Mikhail Ketov
  • Did you hurt the letter "yo"? She also occupies a position in the alphabet. Now you have 32 letters. - Tagir Valeev

3 answers 3

Java-8 First, sort the indexes of the positions in the word in ascending order (we simply compare the characters by the number; if you Collator need, you can Collator ):

 String word = "РАДОСТЬ"; int[] revRanks = IntStream.range(0, word.length()).boxed() .sorted(Comparator.comparing(word::charAt)).mapToInt(Integer::intValue) .toArray(); 

Then we reverse the permutation (stupidly sorting, but it works) and add one:

 int[] ranks = IntStream.range(0, revRanks.length).boxed() .sorted(Comparator.comparingInt(idx -> revRanks[idx])).mapToInt(i -> i+1) .toArray(); System.out.println(Arrays.toString(ranks)); 

Result:

 [4, 1, 2, 3, 5, 6, 7] 

Ideone

  • The result is not what you need, you need not a number in the alphabet, it is easy to do, and since the picture is at the beginning of the post) - Mikhail Ketov
  • @ MikhailKetov is to blame, misunderstood the task Fixed the decision. This is also easy to do. You do not insert pictures better, and write. - Tagir Valeev

I do not know how well and quickly it works, but at least how.

 result.addAll(keyList); // Сортировка по символам for (int i = 0; i < result.size(); i++) { for (int j = 0; j < result.size() - i - 1; j++) { if (result.get(j).getSymbol() > result.get(j + 1).getSymbol()) { Key t = result.get(j); result.set(j, result.get(j + 1)); result.set(j + 1, t); } } } for(int i = 0; i < result.size(); i++){ result.get(i).setNumber(numInKey++); } //Возвращение обратно for(int i = 0; i < keyList.size(); i++){ for(int j = 0; j < result.size(); j++){ if(keyList.get(i).getSymbol() == result.get(j).getSymbol()){ keyList.get(i).setNumber(result.get(j).getNumber()); } } } 
    • We fill HashMap (taking into account the uniqueness of the letters according to the task) with letters, the values ​​are zero.
    • In alphabetical order, in the HashMap, fill in the values ​​with a counter of existing letters.
    • By the letter in the word we can get a serial number in alphabetical order.
    • Profit

    In haste and without the alphabet (for self-solution):

     HashMap<Character, Integer> letters = new HashMap(); for(int i=0;i<input.length();i++){ letters.put(input.charAt(i), 0); } int k=0; for(int j=0;j<'z'-'a';j++){ Character c = Character.toChars('a'+j)[0]; if(letters.containsKey(c)){ letters.put(c,k); k++; } } for(int i=0;i<input.length();i++){ Character c = input.charAt(i); System.out.println(c+" "+letters.get(c)); } 

    https://ideone.com/vPuYfM

    • It’s not much different from my version), I just thought that this task could be solved without such sheet creation / copying - Mikhail Ketov
    • @ MikhailKetov At least you will have O (n ^ 2), I have O (n) - the algorithms are different, I don’t know why they are “not much different”. - Yura Ivanov