Anagrams
Given an array of strings, return all groups of strings that is anagrams.
Note:all inputs'll is in lower-case.
Idea: If this problem is solved, we must know what is a palindrome word-formation. The so-called palindrome word-formation is to change the order of the words, the formation of new words, such as "eat", "tea" is a palindrome formation.
So palindrome word-formation must be the same letter in different order, and there are at least two words.
The subject is to use set to see if the word is repeated after sorting, to determine whether a palindrome word formation. The specific code is as follows:
public class Solution {public list<string> anagrams (string[] strs) {list<string> List = new Arrayl Ist<string> (); if (strs.length <= 1) {return list; } map<string,integer> Map = new hashmap<> (); set<string> set = new hashset<> (); Boolean[] B = new boolean[strs.length];//for each string token, initially false//processing string, into an ordered array of characters for (int i = 0; i < Strs.len gth;i++) {char[] c = Strs[i].tochararray (); Arrays.sort (c); StringBuffer sb = new StringBuffer (); for (char k:c) {sb.append (k);//convert char array to string} if (!set.add (sb.tostring ())) {//the same character array already exists in the list . Add (Strs[i]); int index = Map.get (sb.tostring ()); if (!b[index]) {//has not been added to list List.add (Strs[index]); B[index] = true;//Set the tag to True,}}else{map.put (sb.tostring (), i);//Save the first occurrence of the string index i set.add (sb.tos Tring ());//save set, next judgment is repeated}} return list; }}
The subject at the beginning of the code is very cumbersome, such as the next, as a reference, not with set judgment, so cumbersome, inefficient.
public class Solution {public list<string> anagrams (string[] strs) {list<string> List = new Arrayl Ist<string> (); if (strs.length <= 1) {return list; } map<integer,char[]> Map = new hashmap<> (); Process the string into an ordered array of characters for (int i = 0; i < strs.length;i++) {char[] c = Strs[i].tochararray (); Arrays.sort (c);//Sort Map.put (i, c); } boolean[] B = new boolean[strs.length];//for each string token, initially false for (int k = 0; k < strs.length-1; k++) { if (!b[k]) {//not tagged char[] C0 = Map.get (k);//Assuming that the K string is a palindrome word-building for (int i = k + 1; I &l T strs.length;i++) {//starting from I=1 if (!b[i] && c0.length = = Strs[i].length ()) {//is not judged to be, and is equal to the number of C0 characters char[] C1 = Map.get (i);//array int j = 0; while (J < c0.length) {if (c0[j]! = C1[j]) { break;//if not the same direct break}else{j++;//same +1 }} if (j = = C0.length) {//Description words are all the same List.add (Strs[i]); B[k] = true;//start that also want to mark b[i] = true;//mark is already}} } if (B[k]) {//If the initial is marked, then the list plus List.add (strs[k]); }}} return list; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode 49.Anagrams (palindrome word-formation) thinking and methods of solving problems