每个测试用例每行为一个字符串,以'\n'结尾,例如cccddecca
输出压缩后的字符串ac5d2e
dabcab
a2b2c1d1
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.next(); HashMap<Character,Integer> map = new HashMap(); for(int i = 0;i < str.length();i++){ map.put(str.charAt(i),map.getOrDefault(str.charAt(i),0)+1); } Set set = map.keySet(); Object[] arr = set.toArray(); Arrays.sort(arr); StringBuffer output = new StringBuffer(); for(int i = 0;i < arr.length;i++){ output.append(arr[i] + "" + map.get(arr[i])); } System.out.println(output); } }
import java.io.*; import java.util.HashMap; import java.util.Map; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String str = bf.readLine(); Map<Character,Integer> map = new HashMap<>(); StringBuilder sb = new StringBuilder(); for(int i = 0;i < str.length();i++){ if(map.get(str.charAt(i)) == null){ map.put(str.charAt(i),1); }else{ int temp = map.get(str.charAt(i)) + 1; map.put(str.charAt(i),temp); } } for(char des = 97;des < 123;des++){ if(map.containsKey(des)){ sb.append(des); sb.append(map.get(des)); } } System.out.println(sb.toString()); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] count = new int[256]; String s = scanner.nextLine(); char[] array = s.toCharArray(); for (char c : array) { count[c]++; } StringBuilder builder = new StringBuilder(); for (int i = 'a'; i <= 'z'; i++) { if (count[i]!=0){ builder.append((char)i); if (count[i]!=1) builder.append(count[i]); } } System.out.println(builder.toString()); } }
import java.util.*; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()) { String str=sc.next(); HashMap<String,Integer> map=new HashMap<>(); for(int i=0;i<str.length();i++)//遍历字符串 { char s=str.charAt(i); String ss=""+s; if(map.containsKey(ss))//如果键已经存在了 { int value=map.get(ss);//通过键获取值 value=value+1; map.put(ss,value); } else { map.put(ss,1); } } Set<String> set=map.keySet(); for(String key:set) { int value=map.get(key); System.out.print(key+value); } } } }
package xiaozhao2.bytedance.kuaishou; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class guiyihua { public static void main(String[] args) { Scanner sc = new Scanner(System.in); TreeMap<Character, Integer> treeMap = new TreeMap<>(); String str = sc.next(); char[] chs = str.toCharArray(); for (int i = 0; i < chs.length; i++) { treeMap.put(chs[i], treeMap.getOrDefault(chs[i], 0) + 1); } StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> entry : treeMap.entrySet()) { sb.append(entry.getKey()).append(entry.getValue()); } System.out.println(sb.toString()); } }
糙方法简单实现了一下
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String m=sc.nextLine();
char arr[]=m.toCharArray();
Map<Character,Integer> map =new HashMap<Character, Integer>();
for(int i=0;i<26;i++)
{
char n =(char)(i+97);
map.put(n,0);
}
for(int i=0;i<arr.length;i++)
{
for(Map.Entry<Character,Integer> entry:map.entrySet())
{
if(arr[i]==entry.getKey())
{
//entry.getValue();
map.put(entry.getKey(),entry.getValue()+1);
}
}
}
for(Map.Entry<Character,Integer> entry1:map.entrySet())
{
if(entry1.getValue()!=0)
{
System.out.print(entry1.getKey());
System.out.print(entry1.getValue());
}
}
}
}
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); char[] chars=sc.nextLine().toCharArray(); //经验,一般是用hashmap来统计出现次数的, //但本题是对字符统计次数,可以专门用一个counts数组来统计次数 int[] counts=new int[26]; for(int i=0;i<chars.length;i++){ counts[chars[i]-'a']++; } for(int i=0;i<26;i++){ char c=(char)('a'+i); int count=counts[i]; if(count==0){ continue; }else{ System.out.print(c+""+count); } } } }