Golang string verb modifiers

Source: Internet
Author: User
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.

    • Golang implementation
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; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.