Today I received an interview question: given an array (e.g. ["Cars", "thing", "Scar", "dog", "God", "arcs", "the"]), need to put A word that consists of upside-down alphabetical order in the same array (the resulting result:[["Cars", "scar", "arcs"], ["thing"], ["Dog", "God"], ["the"]])
That's how I realized it:
package com.mingyisoft.bean.test;import java.util.hashmap;import java.util.map;import java.util.map.entry;import java.util.set;import org.apache.commons.lang3.arrayutils;public class samewordutile {/** * compare two strings for the same composition * * @param sourcestr * @param targetStr * @return */public static boolean Checksimilardegree (STRING&NBSP;SOURCESTR,&NBSP;STRING&NBSP;TARGETSTR) {boolean checkresult = false;if (Sourcestr.length () == targetstr.length ()) {char[] sourcestrchararray = sourcestr.tochararray (); Char[] targetstrchararray = targetstr.tochararray (); int n = targetStrCharArray.length;for (int i = 0; i < n; i++) {checkResult = false;for (int j = 0; j < sourcestrchararray.length; j++) {if (Targetstrchararray[i] == sourcestrchararray[j]) {// Remove the Sourcestrchararray = arrayutils.remove (SOURCESTRCHARARRAY,&NBSP;J) from the array that has been compared to the same; checkresult = true;break;}}}} Return checkresult;} /** * Group processing * @param sourceArray * @return */public static Object[] classify (String[] sourcearray) {// gets the number of arrays int sourcearraylength = sourcearray.length;// declares a temporary map, which is used to store the resulting array. Map<string, string[]> tempmap = new hashmap<string, string[]> (); boolean hassame = false;for (int i = 0; i < sourcearraylength; i++) {hasSame = false;if (i == 0) {// If it is the first string of the array, the value of the map is initialized tempmap.put (sourcearray[i], new string[] { sourcearray[0] }); else {// is not the first array, then it begins to compare set<entry<string, string[]>> entryset = tempmap.entryset ();for (entry<string, string[]> entry : entryset) {string keystring = entry.getkey ();// determine if a similar if is already present ( Checksimilardegree (Keystring, sourcearray[i])) {// similar, put similar in the corresponding array entry.setvalue ( Arrayutils.add (Tempmap.get (keystring), sourcearray[i]); hassame = true;break;}} if (!hassame) {// no similar, create a new string Key-value,key use the array, value takes a new array (containing the string) Tempmap.put (sourcearray [i], new string[] { sourcearray[i] });}} Return tempmap.values (). ToArray ();} Public static void main (String[] args) {string[] sourcearray = new String[] { "Cars", "thing", "Scar", "dog", "God", "arcs", "the", "Het" };object[] targetobjectarr = classify (Sourcearray); for (object tempobj: Targetobjectarr) {string[] ss = (string[]) tempObj; SYstem.out.println (arrayutils.tostring (ss));}}}
This article is from the "Sichenming" blog, make sure to keep this source http://yujie020.blog.51cto.com/2827685/1618964
Group array of strings according to the same letter of the word