java 字謎_計算字謎的出現次數

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只是每次提取的子字符串。 窗口長度等于字長。

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 字謎

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377630.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377630.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377630.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Origin繪制熱重TG和微分熱重DTG曲線

一、導入數據 二、傳到Origin中 三、熱重TG曲線 temp為橫坐標、mass為縱坐標 繪制折線圖 再稍微更改下格式 字體加粗&#xff0c;Times New Roman 曲線寬度設置為2 橫縱坐標數值格式為Times New Roman 根據實際情況改下橫縱坐標起始結束位置 四、微分熱重DTG曲線 點擊曲線…

【嵌入式系統復習】嵌入式網絡與協議棧

目錄開放式系統互連模型總線通信的報文組形式以及傳遞方式報文組形式報文傳遞方式網絡分配與調度嵌入式TCP/IP藍牙技術藍牙的節能狀態糾錯方案藍牙協議棧開放式系統互連模型 ISO/OSI七層模型展示了網絡結構與各層的功能。 應用層&#xff1a; 提供了終端用戶程序和網絡之間的應…

代碼兼容、技巧

代碼兼容、技巧 前端開發中&#xff0c;一個頭疼的事&#xff0c;就是代碼的不兼容&#xff0c;這里貼出自己在前端開發中的一些解決經驗。除了其瀏覽器本身的BUG外&#xff0c;不建議使用CSS hack來解決兼容性問題的。 IE和FF下對”li“的的高度解析不同 可以不定義高度&#…

Windows Phone 7 自定義事件

在Windows Phone的應用開發里面&#xff0c;對于事件這種東西我們可以隨處可見&#xff0c;系統本來就已經封裝好了各種各樣的事件機制&#xff0c;如按鈕的單擊事件等等的。在實際的開發中&#xff0c;我們需要自己去給相關的類自定義一些事件來滿足業務的要求&#xff0c;特別…

getcwd函數_PHP getcwd()函數與示例

getcwd函數PHP getcwd()函數 (PHP getcwd() function) The full form of getcwd is "Get Current Working Directory", the function getcwd() is used to get the name of the current working directory, it does not accept any parameter and returns the curren…

十四、數據庫的導出和導入的兩種方法

一、以SQL腳本格式導出&#xff08;推薦&#xff09; 導出 右擊需要導出的數據庫&#xff0c;任務—>生成腳本 下一步 選擇要導出的數據庫&#xff0c;下一步 內容根據需求修改&#xff0c;沒啥需求直接下一步 勾選 表 勾選需要導出的數據庫中的表 選擇腳本保存的路…

Apache中 RewriteCond 規則參數介紹

RewriteCond就像我們程序中的if語句一樣&#xff0c;表示如果符合某個或某幾個條件則執行RewriteCond下面緊鄰的RewriteRule語句&#xff0c;這就是RewriteCond最原始、基礎的功能&#xff0c;為了方便理解&#xff0c;下面來看看幾個例子。RewriteEngine onRewriteCond %{HTT…

【C++grammar】文件I/O流的基本用法

目錄1、輸入輸出類介紹1.C/C文件操作對比2.什么是流&#xff1f;3.C I/O流類層次4.帶緩沖的輸入輸出5.gcc編譯器cin.in_avail()2、向文件寫入數據1.寫文件小練習2.如何將信息同時輸出到文件和屏幕&#xff1f;3、從文件讀數據1.檢測文件是否成功打開2.檢測是否已到文件末尾3.讀…

作業2 分支循環結構

書本第39頁 習題2 1.輸入2個整數num1和num2.計算并輸出它們的和&#xff0c;差&#xff0c;積&#xff0c;商&#xff0c;余數。 //輸入2個整數num1和num2.計算并輸出它們的和&#xff0c;差&#xff0c;積&#xff0c;商&#xff0c;余數。//#include<stdio.h> int main…

求一個序列中最大的子序列_最大的斐波那契子序列

求一個序列中最大的子序列Problem statement: 問題陳述&#xff1a; Given an array with positive number the task to find the largest subsequence from array that contain elements which are Fibonacci numbers. 給定一個具有正數的數組&#xff0c;任務是從包含菲波納…

十三、系統優化

系統整體框架圖 程序運行進入紡織面料庫存管理系統主頁面 用戶子系統功能演示&#xff1a; 1&#xff0c;點擊用戶登錄進入用戶登錄頁面&#xff0c;可以注冊和找回密碼 2&#xff0c;注冊新用戶&#xff0c;賬號、密碼、性別、手機號均有限制&#xff0c;用戶注冊需要按指定…

時間工具類[DateUtil]

View Code 1 package com.ly.util;2 3 import java.text.DateFormat;4 import java.text.ParseException;5 import java.text.SimpleDateFormat;6 import java.util.Calendar;7 import java.util.Date;8 9 /**10 * 11 * 功能描述12 * 13 * authorAdministrator14 * Date Jul 19…

JQuery delegate多次綁定的解決辦法

我用delegate來控制分頁&#xff0c;查詢的時候會造成多次綁定 //前一頁、后一頁觸發 1 $("body").delegate("#tableFoot a:not(a.btn)", "click", function () { 2 _options.page $(this).attr("page"); 3 loadTmpl(_option…

leetcode 45. 跳躍游戲 II 思考分析

題目 給定一個非負整數數組&#xff0c;你最初位于數組的第一個位置。 數組中的每個元素代表你在該位置可以跳躍的最大長度。 你的目標是使用最少的跳躍次數到達數組的最后一個位置。 示例: 輸入: [2,3,1,1,4] 輸出: 2 解釋: 跳到最后一個位置的最小跳躍數是 2。 從下標為 …

C程序實現冒泡排序

Bubble Sort is a simple, stable, and in-place sorting algorithm. 氣泡排序是一種簡單&#xff0c;穩定且就地的排序算法。 A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is pre…

一、爬蟲基本概念

一、爬蟲根據使用場景分類 爬蟲&#xff1a; 通過編寫程序&#xff0c;模擬瀏覽器上網&#xff0c;讓其去互聯網上抓取數據的過程。 ① 通用爬蟲&#xff1a;抓取系統重要的組成部分&#xff0c;抓取的是一整張頁面的數據 ② 聚焦爬蟲&#xff1a;建立在通用爬蟲的基礎之上&am…

經營你的iOS應用日志(二):異常日志

如果你去4S店修車&#xff0c;給小工說你的車哪天怎么樣怎么樣了&#xff0c;小工有可能會立即搬出一臺電腦&#xff0c;插上行車電腦把日志打出來&#xff0c;然后告訴你你的車發生過什么故障。汽車尚且如此&#xff0c;何況移動互聯網應用呢。 本文第一篇&#xff1a;經營你的…

Discuz 升級X3問題匯總整理

最近一段時間公司的社區垃圾帖數量陡然上漲&#xff0c;以至于社區首頁的推薦版塊滿滿都是垃圾帖的身影&#xff0c;為了進一步解決垃圾帖問題我們整整花了1天時間刪垃圾貼&#xff0c;清除不良用戶&#xff0c;刪的手都酸了&#xff0c;可見垃圾帖的數量之多&#xff01;可恥的…

【C++grammar】格式化輸出與I/O流函數

目錄1、格式化輸出1. setw manipulator(“設置域寬”控制符)2. setprecision manipulator(“設置浮點精度”控制符)3. setfill manipulator(“設置填充字符”控制符)4. Formatting Output in File Operation(在文件操作中格式化輸入/輸出)5.小練習2、用于輸入/輸出流的函數1. g…

python 忽略 異常_如何忽略Python中的異常?

python 忽略 異常什么是例外&#xff1f; (What is an Exception?) An exception is an event, which occurs during the execution of a program that interrupts the normal execution of the application. Generally, any application when encountered with a situation t…