java 字谜

Problem statement:

问题陈述:

Given a string S and a word C, return the count of the occurrences of anagrams of the word in the text. Both string and word are in lowercase letter.

给定一个字符串S和一个单词C ,返回该单词在文本中的字谜出现的次数 。 字符串和单词均为小写字母。

Examples:

例子:

    Input:
S=fororfrdofr
C=for
Output:
3
Input:
S=aabaabaa
C=aaba
Output:
4

Example with explanation:

带有说明的示例:

Anagrams:

字谜:

Two words are known to be anagrams of each other if they are of same length & use same set of characters with same frequencies.

如果两个单词的长度相同并且使用相同频率的相同字符集,则两个单词彼此称为字谜。

"aba" and "baa" are anagrams since both have two a's and one b.

“ aba”“ baa”是七巧板,因为它们都有两个a和一个b

"aba" and "bab" are not anagrams.

“ aba”“ bab”不是字谜。

For the First example:

对于第一个示例:

    Input:
S=fororfrdofr
C=for
S:
0   1   2   3   4   5   6   7   8   9   10
f   o   r   o   r   f   r   d   o   f   r
S(0,2): for
Thus S(0,2) is anagram of w
S(3,5) : orf
Thus S(3,5) is anagram of w
S(8,10) : ofr
Thus S(8,10) is anagram of w
Thus count of occurrences of anagrams: 3

Pre-requisite:

先决条件:

  1. FUNCTION to check whether to word is anagram or not

    检查单词是否为字谜的功能

    Check(string s1, string s2): returns true or false based or anagram detection

    Check(字符串s1,字符串s2) :返回基于true或false的或字谜检测

  2. String::substr(int i,int j): returns substring of length j from index i

    String :: substr(int i,int j) :从索引i返回长度为j的子字符串

Algorithm:

算法:

    1.  Set count=0;
For i=0:i<a.length()-b.length()+1
IF (check(a.substr(i,b.length()),b))
count++;
2.  Print count

So the idea is pretty simple. What we are doing is to slide the window and check for anagrams. Window is nothing but the substring being extracted every time. Window length is equal to the word length.

所以这个想法很简单。 我们正在做的是滑动窗口并检查字谜。 Window只是每次提取的子字符串。 窗口长度等于字长。

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

To check anagrams

检查字谜

FUNCTION check(string a, string b)
1.  Declare a map m; //to store frequency of each character
2.  For string abuild the map
For (int i=0;i<a.length();i++)
m[a[i]]++;
END FOR
3.  For string bdestruct the map.
For (int i=0;i<b.length();i++)
m[b[i]]--;
END FOR
4.  If map is totally destructed that means all key has value perfectly 0
return true;
Else
Return false.
END FUNCTION

So this basically means, we are constructing a container using the elements of string a. Then we are destructing the map to form string b. If such formation is possible they strings are anagrams of each other.

因此,这基本上意味着,我们正在使用字符串a的元素构造一个容器。 然后,我们将映射销毁以形成字符串b。 如果这样的形成是可能的,则它们的弦是彼此的字谜。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
bool check(string a,string b){//checking anagrams
map<char,int> m;
for(int i=0;i<a.length();i++)
m[a[i]]++;
for(int i=0;i<b.length();i++)
m[b[i]]--;
for(auto it=m.begin();it!=m.end();it++)
if(it->second!=0)
return false;
return true;
}
int countNoOfAnagram(string a,string b){int count=0;
//sliding window
for(int i=0;i<a.length()-b.length()+1;i++){if(check(a.substr(i,b.length()),b))
count++;
}
return count;
}
int main()
{string S,C;
cout<<"Enter string S and word C\n";
cin>>S>>C;
cout<<"No of anagrams: "countNoOfAnagram(S,C)<<endl;
return 0;
}

Output

输出量

Enter string S and word C
fororfrdofr
for
No of anagrams: 3

翻译自: https://www.includehelp.com/icp/count-occurrences-of-anagrams.aspx

java 字谜

java 字谜_计算字谜的出现次数相关推荐

  1. java 球面距离_计算球面镜焦距的Java程序

    以下是球面镜焦距的Java代码- 示例import java.util.*; import java.lang.*; public class Demo{ public static float co ...

  2. java 八字_计算生辰八字的C#或者java的Demo

    展开全部 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...

  3. 字谜大全及答案100个_检查字谜(检查两个字符串是否是字谜)

    字谜大全及答案100个 Problem statement: Given two strings, check whether two given strings are anagram of eac ...

  4. Java黑皮书课后题第7章:**7.3(计算数字的出现次数)编写程序,读取1到100之间的整数,然后计算每个数出现的次数。假定输入0表示结束

    7.3(计算数字的出现次数)编写程序,读取1到100之间的整数,然后计算每个数出现的次数 题目 题目描述+运行示例 破题 法一 法二 代码 法一:硬生生解出来 法二完整代码 题目 题目描述+运行示例 ...

  5. java程序如何获取北京时间_计算当前的北京时间java(currentTimeMillis)

    计算当前的北京时间java(currentTimeMillis) 计算当前的北京时间java(currentTimeMillis) 当前北京时间 编写一个显示当前GMT时间的程序,该时间的格式为小时: ...

  6. 计算Java List中的重复项出现次数【转】

    本文演示如何使用Collections.frequency和Map来计算重复项出现的次数.(Collections.frequency在JDK 1.5版本以后支持) package com.qiyad ...

  7. java计算任意2个日期内的工作日_计算任意2个日期内的工作日(摘抄)

    思路不错,对时间的操作挺全面,参考一下.代码如下: package test; import java.text.SimpleDateFormat; import java.util.Calendar ...

  8. java 编程求图形面积_求java编程,计算长方形面积?

    求java编程,计算长方形面积? mip版  关注:195  答案:2  悬赏:0 解决时间 2021-01-28 06:47 已解决 2021-01-27 08:26 求java编程,计算长方形面积 ...

  9. java 两日期的周数_利用 Java 中 Calendar 计算两个日期之间的天数和周数

    利用 Java 中 Calendar 计算两个日期之间的天数和周数 前言 究竟什么是一个 Calendar 呢? 中文的翻译就是日历, 那我们立刻可以想到我们生活中有阳 (公) 历阴 (农) 历之分它 ...

最新文章

  1. 【数据挖掘】神经网络 后向传播算法 ( 线性回归与逻辑回归 | 单个神经元本质及逻辑 | 神经网络每一层分析 | 神经网络矩阵形式 | 线性变换与非线性变换 )
  2. 聊聊hystrix的execution.isolation.semaphore.maxConcurrentRequests属性
  3. Winform中通过代码设置DevExpress的TextEdit的类型为Numbernic
  4. 2015-03-17 current note creation logic in my task
  5. [渝粤教育] 中国地质大学 高层建筑施工 复习题
  6. [css] absolute的containing block(容器块)计算方式和正常流有什么区别?
  7. php字符集转换,php字符集转换
  8. 前端系统化学习【JS篇】:(三)Javascript中的命名规范
  9. 安徽省2012年下半年计算机水平考试(二级 c语言程序设计),安徽省计算机等级级考试真题C语言2012年12月.doc...
  10. Object-c 学习笔记
  11. SQL语句——查询语句
  12. C语言工程实践--物业费管理系统
  13. 深度可分离卷积vs标准卷积
  14. CSC公派访问学者申请条件是什么?
  15. wed是什么意思在计算机应用基础中,卡西欧wed什么意思
  16. 记录TI电量计采集化学ID过程
  17. 会计凭证、成本中心、成本要素、总账、物料标准成本核算
  18. android开发者选项打开方式,打开、关闭安卓手机的开发者选项的方法详解
  19. 大数据、物联网、云计算
  20. 由MAVEN入手浅谈项目构建与管理

热门文章

  1. GetWeApp聊天室 代码(微信小程序)
  2. es6 includes(), startsWith(), endsWith()
  3. MySql 踩坑小记 1
  4. python 函数式编程尾递归优化 day16
  5. python—内置函数-字符串,eval,isinstance
  6. Redis配置文件配置
  7. Java基础5一数组的常见应用算法
  8. 读《深入分析Java Web技术内幕》
  9. hdu2115: I Love This Game
  10. 病毒的手工排除与分析(更新完毕)