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. 记录一些容易忘记的属性 -- NSTimer

    使定时器停止的方法: 1. //将定时器的启动时间设置为很久以后的将来,到这个时间,定时器才会开始工作            [_timer setFireDate:[NSDate distantFu ...

  2. 笔记本安装Win2012R2 心得(包含无线网卡+有线网卡驱动解决方法)

    笔记本:联想昭阳E47G 无线网卡安装方法: 系统安装完毕后将自动识别无线网卡驱动,但需要手动允许WLAN服务开启.(建议,如果是拿来办公或者家用,可以安装上桌面体验)不然,QQ发的截图双击都看不起. ...

  3. Swift 实现iOS Animation动画教程

    这是一篇翻译文章.原文出处:http://www.raywenderlich.com/95910/uiview-animation-swift-tutorial 动画( animation)是iOS用 ...

  4. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  5. NSString和data转换

    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NS ...

  6. XML元素和结点的区别

    1.区别介绍 Element是Node的扩展,所以也更实用一些.例如,用Element可以方便的获得Node的属性getAttribute(String attrName)如果用Node,可以得到一个 ...

  7. N的阶乘的长度 V2(斯特林近似) 求 某个大数的阶乘的位数 .

    求某个大数的阶乘的位数 . 得到的值  需要 +1 得到真正的位数 斯特林公式在理论和应用上都具有重要的价值,对于概率论的发展也有着重大的意义.在数学分析中,大多都是利用Г函数.级数和含参变量的积分等 ...

  8. html网页标题

    HTML代码 <html> <head> <!--<title>定义网页标题,显示在浏览器的标题--> <title>网页标题</ti ...

  9. 自定义NavigationBar

    [self.navigationController.navigationBarsetHidden:YES]; CGRect screenRect = [[UIScreen mainScreen] b ...

  10. HttpHandler中无法使用Session(context.Session 为Null)

    一.问题描述: 最近需要做一个对特定请求(.report)进行响应的接口,当然是使用实现IHttpHandler来进行处理,实现IHttpHandler接口就必须要实现它的两个方法,ProcessRe ...