This is a creation in Article, where the information may have evolved or changed.
Goal
Write a function anagram (s, t) to determine whether the two strings are in reverse alphabetical order.
func solution(s , t string)bool{ if s == t { return true } length := len(s) if length != len(t) { return false } //' ' 32 --> ~ 126 const MAX_ASCII int= 94 const SPACE_INDEX rune = 32 numbers := [MAX_ASCII]int{} sRune := []rune(s) tRune :=[]rune(t) for i := 0 ; i < length ; i++ { index := tRune[i] - SPACE_INDEX numbers[index]++ index = sRune[i] - SPACE_INDEX numbers[index]-- } for i := 0 ; i < MAX_ASCII / 2 ; i++{ mergeSize := numbers[i] if mergeSize != 0 || mergeSize != numbers[MAX_ASCII - 1 - i]{ return false } } return true}
Where key point 1:
Defines a value that holds the length of the last two strings that are judged to be identical:
According to the ASCII table you can know:
The first single character "of the 10 binary value bit 32, the last single character ' ~ ' 10 binary Value bit 126, the difference between the resulting value is 94,
Each character is predicted to be used here, so the length is directly defined as 94.
- The Java implementation is similar to the above:
public Boolean anagram (string s, String t) {if (s = = NULL | | t = NULL | | s.length () ==0 | | S.length ()! = T.length ()) {return false; } if (S.equals (t)) return true; Final int max_ascii = 94; Final char space_index = '; int[] numbers = new INT[MAX_ASCII]; int length = S.length (); char[] Schararray = S.tochararray (); char[] Tchararray = T.tochararray (); for (int i = 0; i< length; i++) {int index = schararray[i]-Space_index; numbers[index]++; index = tchararray[i]-Space_index; numbers[index]--; } for (int i =0; i < MAX_ASCII/2; i++) {int mergesize = numbers[i]; if (mergesize! = 0 | | Mergesize! = numbers[max_ascii-1-i]) {return false; }} return true; }