Print the count of duplicate char in a given string in same order. Ex: Input- 'abbaccdbac', Output- 'a3b3c3d1'
import java.util.LinkedHashMap;
import java.util.Map;

/*Print the count of duplicate char in a given string in same order. Ex: Input- 'abbaccdbac', Output- 'a3b3c3d1'
 *
 */

public class Solution {

	public static void main(String[] args) {
		String s="asbvcabc";
		System.out.println(new Solution().countDuplicate(s));

	}

	public String countDuplicate(String s){
		StringBuilder strBuilder=new StringBuilder();
		Map<Character, Integer> map=new LinkedHashMap<Character, Integer>();
		for(int i=0; i<s.length(); i++){
			if(map.containsKey(s.charAt(i))){
				int value=map.get(s.charAt(i))+1;
				map.put(s.charAt(i), value);
			}
			else{
				map.put(s.charAt(i), 1);
			}
		}
		for(Map.Entry<Character, Integer> entry: map.entrySet()){
			strBuilder.append(entry.getKey());
			strBuilder.append(entry.getValue());
		}
		String res=strBuilder.toString();
		return res;
	}
}

  注意题意,要选用对的数据结构

Amazon-countDuplicate的更多相关文章

  1. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(3): 抓取amazon.com价格

    通过上一篇随笔的处理,我们已经拿到了书的书名和ISBN码.(网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(2): 抓取allitebooks.com书籍信息 ...

  2. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(2): 抓取allitebooks.com书籍信息及ISBN码

    这一篇首先从allitebooks.com里抓取书籍列表的书籍信息和每本书对应的ISBN码. 一.分析需求和网站结构 allitebooks.com这个网站的结构很简单,分页+书籍列表+书籍详情页. ...

  3. 网络爬虫: 从allitebooks.com抓取书籍信息并从amazon.com抓取价格(1): 基础知识Beautiful Soup

    开始学习网络数据挖掘方面的知识,首先从Beautiful Soup入手(Beautiful Soup是一个Python库,功能是从HTML和XML中解析数据),打算以三篇博文纪录学习Beautiful ...

  4. Amazon Interview | Set 27

    Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...

  5. cosbench read异常解决办法。 Unable to verify integrity of data download. Client calculated content hash didn't match hash calculated by Amazon S3. The data may be corrupt.

    问题:cosbench read测试failed 报错如下 Cosbench v0.4.2.c4 against Ceph (Hammer) / radosgw / HAproxy's HTTP en ...

  6. [Amazon] Amazon IAP for Unity

    1> 下载amazon IAP3.0 for unity plugin 2> 根据 https://developer.amazon.com/public/apis/earn/in-app ...

  7. Amazon评论数据的预处理代码(Positive & Negative)

    Amazon评论数据的预处理代码,用于情感分析,代码改自 https://github.com/PaddlePaddle/Paddle/tree/develop/demo/quick_start/da ...

  8. Amazon EC2免费VPS防止超额被扣钱三大方法:流量 硬盘读写 运行时长

    Amazon EC2也就是亚马逊云服务免费VPS主机服务,内存是613MB,月流量是30GB,主机空间是30GB,可以免费使用一年,又加上Amazon服务器全球多个节点CDN和本身的名气,早在2010 ...

  9. Amazon AWS 架设EC2服务器(datizi)fanqiang (更新手机VPN/L2TP设置)

    今天用AWS在东京架设了一台服务器用来个人fanqiang.为什么用AWS呢,阿里云学生价9.9可以搭在香港,但是我的学制今年2月份在学信网上就到期了,腾讯云holy shit,我司AZURE据说员工 ...

  10. How to ssh to your Amazon Elastic Beanstalk instance?

    Well, if it's ec2 or a digital ocean server, it would be a lot easier- you do what you normally do f ...

随机推荐

  1. checkbox的全选、反选(计算价格)

    package com.baidu.jisuan; import java.util.ArrayList;import java.util.List; import com.baidu.adapter ...

  2. Browser GetImage

    using Microsoft.Win32; using System; using System.ComponentModel; using System.Drawing; using System ...

  3. 如何使用SVN管理我们的源代码

    今天把公司的SVN服务器配置给做了一下,根据公司部门的不同,划分了不同的访问目录,并给不同目录配置了相应的权限,算是把这份差事给干完了,但其实我对自己的工作是不满意和有遗憾的,因为目前公司的SVN服务 ...

  4. 找第k大的数

    (找第k大的数) 给定一个长度为1,000,000的无序正整数序列,以及另一个数n(1<=n<=1000000),接下来以类似快速排序的方法找到序列中第n大的数(关于第n大的数:例如序列{ ...

  5. ZOJ 2672 Fibonacci Subsequence(动态规划+hash)

    题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai.并输出这个序列. 很容易想到一个DP方程 dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j], ...

  6. Shell获取当前用户

    id | sed -e 's/).*//g' -e 's/.*(//' 比$LOGNAME $NAME who am i都要准确一些

  7. CWNP宣布中国首位CWNE获得者——朱志立(Kevin Zhu)

    CWNP宣布中国首位CWNE获得者——朱志立(Kevin Zhu)   Kevin Zhu获得了全球CWNE认证无线网络专家的第134号,CWNE被公认为IT行业最难获取的10大认证之一.     [ ...

  8. (转)html5开发之viewport使用

    原文:http://www.php100.com/html/webkaifa/HTML5/2012/0831/10979.html 随着高端手机(Andriod,Iphone,Ipod,WinPhon ...

  9. context理解

    官方文档的解释是:Context提供了关于应用环境全局信息的接口.它是一个抽象类,它的执行被Android系统所提供.它允许获取以应用为特征的资源和类型.同时启动应用级的操作,如启动Activity, ...

  10. HDU 5820 (可持久化线段树)

    Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...