python 游程编码

Problem description

问题描述

Write a program that counts frequency of each letter in the string (string consists lowercase letters only).

编写一个程序来计算字符串中每个字母的频率 (字符串仅包含小写字母)。

Solution:

解:

Algorithm

算法

  1. Initialize an array of 26 elements for each letter (a-z) to 0. (array[26]={0})

    将每个字母(az)初始化为26个元素的数组,以将其设置为0。( array [26] = {0} )

  2. Scan the entire string and for each string element check the letter and increase the frequency in array by using ASCII value. (array[str[i]-'a']++)

    扫描整个字符串,并为每个字符串元素检查字母,并使用ASCII值增加数组中的频率。 ( array [str [i]-'a'] ++ )

    Like in

    str="aaabbccccddef",

    str =“ aaabbccccddef” ,

    str [3] ='b'

    str [3] ='b'

    Thus, and

    因此,

    str [2]-'a'=1

    str [2]-'a'= 1

    Thus it increases the frequency of

    因此,它增加了频率

    'b' by 1 (array [str [3]-'a'] turns to be array [1] ++)

    “ b”乘以1(数组[str [3]-'a']变成数组[1] ++ )

  3. Finally print the letter with their respective frequencies. This is the encoded string.

    最后,以相应的频率打印字母。 这是编码的字符串。

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C ++程序查找/打印字符串中字母的频率 (C++ program to find/print frequency of letters in a string)

#include <bits/stdc++.h>
using namespace std;
void freq(string s){//array to store frequency of 26 characters,initialized to 0
int arr[26]={0};
for(int i=0;i<s.length();i++){// s[i] is the ascii value of the letter at index i & 'a'
//also gives the ascii value of a, in this way we are
//checking which alphabet is at index i and increasing its frequency
arr[s[i]-'a']++;
}
for(int i=0;i<26;i++){
if(arr[i]!=0)
printf("%d%c",arr[i],'a'+i);
}
cout<<endl;
}
int main(){string s;
cout<<"enter string\n";
cin>>s;
cout<<"encoded string is : "<<endl;
freq(s);
return 0;
}

Output

输出量

enter string
aaaabbcccccddf
encoded string is :
4a2b5c2d1f

Recommended posts

推荐的帖子

  • Checking Anagrams (check whether two string is anagrams or not)

    检查字谜(检查两个字符串是否是字谜)

  • Count and Say sequence

    计数并说出顺序

  • Longest Common Prefix

    最长的公共前缀

  • Count Substrings

    计数子串

  • Number following the pattern

    跟随模式的数字

  • Next Permutation

    下一个排列

  • Convert Ternary Expression to Binary Tree

    将三元表达式转换为二叉树

  • Count of strings that can be formed using a, b and c under given constraints

    在给定约束下可以使用a,b和c形成的字符串数

  • Minimum Number of Flips

    最小翻转次数

  • Count Occurrences of Anagrams

    计算字谜的出现次数

  • Rearrange a string

    重新排列字符串

  • Print bracket number

    列印支架编号

  • Longest Palindromic Subsequence

    最长回文序列

  • Preorder to Postorder of BST

    BST的预购到后购

  • Maximum difference of zeros and ones in binary string

    二进制字符串中零和一的最大差

翻译自: https://www.includehelp.com/icp/run-length-encoding-print-frequency-of-letters-in-a-string.aspx

python 游程编码

python 游程编码_游程编码(字符串中字母的查找/打印频率)相关推荐

  1. python字典统计字母出现次数_第三篇 python运用字典统计字符串中字母出现的次数-Go语言中文社区...

    碎碎念 这个内容还是我日常互相种草的好友提供的素材,很基础也很实用,稍微进阶一些就可以用来统计文章中的单词出现的频率了.她在网上找的代码用了库,通过python中的字典可以很简洁的完成.(下图是她在网 ...

  2. python统计表中单词及其出现的次数 字典形式输出_统计字符串中字母出现的次数,字典形式输出(python)...

    a = "aAsmr3idd4bgs7Dlsf9eAF" 请将a字符串的数字取出,并输出成一个新的字符串. 请统计a字符串出现的每个字母的出现次数(忽略大小写,a与A是同一个字母) ...

  3. python使用正则表达式删除字符串中的其它字符只保留数字和字母

    python使用正则表达式删除字符串中的其它字符只保留数字和字母 #python使用正则表达式删除字符串中的其它字符只保留数字和字母 # Python code to demonstrate # to ...

  4. python 正则之提取字符串中的汉字,数字,字母

    python 正则之提取字符串中的汉字,数字,字母 #\d 匹配一个数字字符.等价于 [0-9] #\D 匹配一个非数字字符.等价于 [^0-9]#过滤字符串中的英文与符号,保留汉字 import r ...

  5. python upper()函数和lower()函数(返回字符串中字母的大[小]写)(大写、小写)

    upper()方法 Python upper() 方法将字符串中的小写字母转为大写字母. str.upper() #!/usr/bin/python3str = "this is strin ...

  6. python删除字符串中重复字符_删除字符串中重复字符python 用CAD怎么画DNA反向

    用CAD怎么画DNA反向平行双螺旋结构绘螺旋线时,用选扭曲,确定顺时针. 画双头螺旋线时,第二根螺旋线底圆起点与第一根螺旋线底圆起点,可用角度分隔如180°.python去除文本中重复的字符串可有可无 ...

  7. python统计文字个数_python如何统计字符串中字母个数?

    方法:首先用"str_count = 0"定义字母的字符初始个数为0:接着遍历字符串,判断字符串内各字符的类型,并将字母个数累加:最后用"print('字母 = %d' ...

  8. 【python作业】编写一个函数,由实参传来一个字符串,统计此字符串中字母和数字的个数,在主函数中输入字符串并输出上述的结果。

    编写一个函数,由实参传来一个字符串,统计此字符串中字母和数字的个数,在主函数中输入字符串并输出上述的结果. def fun(s):count1 = 0count2 = 0for i in s:if i ...

  9. Java黑皮书课后题第6章:*6.20(计算一个字符串中字母的个数)编写一个方法,使用下面的方法体计算字符串中的字母个数。编写一个测试程序,提示用户输入字符串,然后显示字符串中的字母个数

    6.20(计算一个字符串中字母的个数)编写一个方法,使用下面的方法体计算字符串中的字母个数.编写一个测试程序,提示用户输入字符串,然后显示字符串中的字母个数 题目 题目描述 破题 代码 运行示例 题目 ...

最新文章

  1. wordpress 新建php文件大小,WordPress最大上传文件大小限制修改
  2. 315. Count of Smaller Numbers After Self 计算右侧小于当前元素的个数
  3. java j2se1.5_用J2SE1.5建立多任务的Java应用程序...
  4. es 同义词 热更新 1.1版本
  5. 蓄电池单格电压多少伏_蓄电池充电规范手册
  6. Playing with Permutations(CF-252D)
  7. eth的geth钱包安装
  8. 显卡(N卡)内存被占用如何处理?
  9. 生信识图 之 点图基础
  10. 关于pict工具进行测试用例的自动生成过程中:使用 pict.exe test.txt >test.xsl 导出为xls格式的表格文件时,出现拒绝访问的提示解决方案
  11. 大明湖畔昇腾绽放,趵突泉里智能奔涌
  12. 数据结构(一):数组
  13. 【摘记】彼得·林奇的成功投资
  14. 猴子偷桃(Java实现)
  15. java四目运算符_小心! JAVA三目运算符
  16. oracle 如何考试,oracle考试怎么报名
  17. thingsboard 规则引擎结点功能总结
  18. GOROOT 和 GOPATH 的区别
  19. 安装前端编辑器HBuilderX
  20. 第 46 届 ICPC 国际大学生程序设计竞赛亚洲区域赛(上海)(热身赛(A/B) + 正式赛(D/E))

热门文章

  1. ClearCase指南-基础篇(连载一)
  2. 服务器硬盘ahci,Windows2008 AHCI功能开启方法(提升硬盘加速)
  3. 钉钉低代码是什么,能干什么?
  4. 2021款 联想拯救者R7000P
  5. DM数据库使用dmmdf工具修改db_magic
  6. 接口测试:HTTP协议中的状态码
  7. NumPy 成绩统计
  8. 【超图+CESIUM】【基础API使用示例】38、超图|CESIUM - 特效-云层设置
  9. Park变换输入输出前后都是交流量的问题及解决办法
  10. 非功能测试-数据库awr报告分析