[虚拟机OA]Fun with Anagrams 玩转同构词

Two strings are anagrams if they are permutations of each other. For example, "aaagmnrs" is an anagram of "anagrams". Given an array of strings, remove each string that is an anagram of an earlier string, then return the remaining array in sorted order.

For example, given the strings s = [‘code‘, ‘ecod‘, ‘framer‘, ‘frame], the strings ‘doce‘ and ‘ecod‘ are both anagrams of ‘code‘ so they are removed from the list. The words ‘frame‘ and ‘framer‘ are not anagrams due to the extra in ‘framer‘, so they remain. The final list of strings in alphabetical order is [‘code‘, ‘frame‘, framer].

Function Description

Complete the function funWithArtagrams in the editor below. It must return a list of strings in alphabetical order, ascending.

funWithAnagrams has the following parameters:

s [s[0]...s[n-1]]: an array of strings

Sample Case 0
Sample Input For Custom Testing
4
code
aaagmnrs
anagrams
doce

Sample Output
aaagmnrs
code 

Explanation
aaagmnrs and anagrams are anagrams, code and doce are anagrams. After sorting aaagmnrs comes first.

题意:

思路:

代码:

 1  public static List<String> funWithAnagrams(List<String> s){
 2         TreeSet<String> ans = new TreeSet<>();
 3         Map<String, String> map = new HashMap<>();
 4         for(String str : s){
 5             String ana = toAnagram(str);
 6             if(map.containsKey(ana)){
 7                 continue;
 8             }else{
 9                 map.put(ana, str);
10                 ans.add(str);
11             }
12         }
13         return new ArrayList<>(ans);
14
15     }
16
17     private  static String toAnagram(String s){
18         int[] ans = new int[26];
19         for(char c : s.toCharArray()){
20             ans[c - ‘a‘] ++;
21         }
22         StringBuilder sb = new StringBuilder();
23         for (int i = 0; i < 26 ; i++) {
24             while(ans[i] -- > 0){
25                 sb.append((char)(‘a‘ + i));
26             }
27         }
28         return sb.toString();
29     }

原文地址:https://www.cnblogs.com/liuliu5151/p/11509826.html

时间: 2024-10-07 13:14:33

[虚拟机OA]Fun with Anagrams 玩转同构词的相关文章

[虚拟机OA]Group Anagram 变位词归类

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

[虚拟机OA]Maximal Square 最大正方形

Given a 2D binary matrix filled with 0's and 1's,find the largest square containing only 1's and return its area. Input: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0 Output: 4 (参考LeetCode 221 Maximal Square) 思路: dp[i][j] 代表在以i, j 这个点为右下角的正方形变成若这一格的值是1, 则这个正方

[虚拟机OA]Break a Palindrome 破坏回文串

In this challenge, you will be given a palindrome which you must modify if possible. Change exactly one character of the string to another character in the range ascii[a-z] so that the string meets the following two conditions: • The new string is no

[虚拟机OA]Even Subarray 最多含有K个奇数的子数组

A subarray is a contiguous portion of an array. Given an array of integers, you must determine the number of distinct subarrays that can be formed having at most a given number of odd elements. Two subarrays are distinct if they differ at even one po

[虚拟机OA]Climb the hill 爬山

Jack was trying to go up the hill. He does not have any problem in climbing up or coming down the hill if the slope is consistently either increasing or decreasing. Areas where the slope is constant do not bother him in either situation. Given a list

[虚拟机OA]Build the Subsequences 生成子序列

A subsequence of a string is obtained by deleting zero or more characters from the string while maintaining order. For example, the subsequences of string s = "xyz", not including the empty string, are "x", "xy", "xz&quo

[虚拟机OA]Team Formation 2 团队构成

FC Codelona is trying to assemble a team from a roster of available players. They have a minimum number of players they want to sign and each player needs to have a skill rating within a certain range. Given a list of players' skill levels with desir

[Leetcode] Anagrams 颠倒字母构成词

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题意:anagrams的意思是回文构词法.回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序.如: Input: ["tea","and","ate","eat",&qu

通达OA用户操作手册(二)

4.3 业务流程平台 业务流程平台的详细使用介绍,详见<Office Anywhere2017 业务流程平台使用详解>,此处不再赘言. 4.4 工作流 工作流的详细使用介绍见<Office Anywhere2017 工作流使用详解>. 4.4.1 新建工作 图 4.4-1 进入[工作流]-[新建工作],将鼠标放在需要新建的流程后面即可看到[快速新建]和[新建向导]两个按钮,单击进入新建向导界面,双击为快速新建进入工作办理界面.如果用户没有流程第一步的经办权限,那么用户在新建界面看不