Encode Adjacent Letters
Encode a string by counting the consecutive letter.
(i.e., "aaaabbxxxyyz" might become "a4b2x3y2z1").
分析:
这个问题是一个基本的数据压缩算法,将相邻的字符计数存储。解法没有什么特别需要注意的地方,按部就班的实现代码就可以了。
class Solution:
# @param s, letters as string
# @return an encoded string
def encode(self, s):
if not s or len(s) == 0:
return ""
val = ""
prev = s[0]
count = 1
for c in s[1:]:
if c == prev:
count += 1
else:
val += prev + str(count)
prev = c
count = 1
val += prev + str(count)
return val
if __name__ == '__main__':
s = Solution()
assert s.encode("") == ""
assert s.encode("aaaabbxxxyyz") == "a4b2x3y2z1"
assert s.encode("aaaaaaaaaaab") == "a11b1"
print 'PASS'
小结:
extends下是目前不在LeetCode但是我遇到的有趣的程序问题。
Encode Adjacent Letters的更多相关文章
- Bioinformatics Glossary
原文:http://homepages.ulb.ac.be/~dgonze/TEACHING/bioinfo_glossary.html Affine gap costs: A scoring sys ...
- How to Write a Spelling Corrector
http://norvig.com/spell-correct.html Feb 2007to August 2016 How to Write a Spelling Corrector One we ...
- 图论:(Code Forces) Graph and String
Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- IOI 2009:Mecho
IOI2009 Mecho Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Origin ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- Uva227.Puzzle
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 138 Solved: 97 ...
- [Swift]LeetCode809. 情感丰富的文字 | Expressive Words
Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...
- [LeetCode] Expressive Words 富于表现力的单词
Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...
随机推荐
- Spring MVC web.xml+servlet.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...
- bat批处理以当前时间创建文本文件
:: 表示注释 :: @表示不显示当前命令,只在后台执行 :: @echo off 表示以后执行的命令都不显示 :: set d=%,% 表示设置变量d为当前年月日,默认表示为例如:// :: set ...
- Session存储
session其实分为服务器端Session和客户端Session. 当用户首次与Web服务器建立连接的时候,服务器会给用户分发一个sessionid作为标识.用户每次提交页面,浏览器都会把这个ses ...
- PHP处理MySQL事务代码
php使用mysqli进行事务处理 <?php$db = new mysqli("localhost","root",""," ...
- PHP函数前面添加@的作用
@是PHP提供的错误信息屏蔽的专用符号. 比如在一个函数前使用@ @mysql_query 不会出现Warning, 而原来mysql_query 在遇到错误时会在页面上访提示Warning. @是可 ...
- 初识 JVM
发展历史 1996年,SUN JDK 1.0 Classic VM 发布,纯解释运行,使用外挂进行JIT 1997年 JDK1.1 发布.包含了:AWT.内部类.JDBC.RMI.反射 1998年 J ...
- JavaScript高级与面向对象
对象:任何事物都可以看作是对象. 1.面向对象与面向过程的概念 面向过程:凡是自己亲力亲为,自己按部就班的解决现有问题. 面向对象:自己充当一个指挥者的角色,指挥更加专业的对象帮我解决问题. 联系:面 ...
- MVVM模式的3种command总结[1]--DelegateCommand
MVVM模式的3种command总结[1]--DelegateCommand 查了不少资料,大概理清楚的就是有3种.当然类名可以自己取了,不过为了便于记忆和区分,还是和看到的文章里面用一样的类名. 1 ...
- JavaScript 打印Div内容
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Page& ...
- constructor&object 的联系与对比
构造函数与对象 构造函数是类中的特殊成员函数,用于为对象分配内存.它可用于为数据成员提供值.创建对象时将调用构造函数.它与类具有相同的名称.构造函数不返回任何值. 构造函数是生成对象的模板,一个构造函 ...