前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >671. 循环单词重复加标记

671. 循环单词重复加标记

作者头像
和蔼的zhxing
发布于 2018-09-04 03:31:41
发布于 2018-09-04 03:31:41
57700
代码可运行
举报
运行总次数:0
代码可运行

The words are same rotate words if rotate the word to the right by loop, and get another. Count how many different rotate word sets in dictionary.

E.g. picture and turepic are same rotate words.

注意事项

所有单词均为小写。 样例 Given dict = ["picture", "turepic", "icturep", "word", "ordw", "lint"] return 3.

"picture", "turepic", "icturep" are same ratote words. "word", "ordw" are same too. "lint" is the third word that different from the previous two words.

重复加标记

难点在于如何判断是否是循环单词,看到别人的思路:可以把当前单词重复一次,然后所有的循环单词都是可以在这个重复的单词中找到的,其实有点像循环移位和线性移位的关系,周期延拓之后线性移位和循环移位的结果是一样的。 比如对于单词word,先重复一遍得到:wordword. word的循环单词都是wordword的子串,找子串可以借助string::find(s)函数,这样就能判断是否是子串。 这样我们就可以去遍历vector中的单词了,对于第一个单词,扩充,然后在余下的单词中找是循环关系的,找到的应该都是要标记出来的,要不会有重复,可以定义一个vector来标记这个单词是否被找到(找到了在后面就无需遍历了),每完成这样的一次查找,计数器+1,一直遍历到最后一个单词。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
int countRotateWords(vector<string> words)
     {
        int res = 0;
        int sz = words.size();
        string dbCurrent;
        vector<bool> check(sz, false);
        if (words.empty())
        return res;
        for (int i = 0; i < sz; i++)
        {
        if (!check[i])
        {
            dbCurrent = words[i] + words[i];
            for (int j = i + 1; j < sz; j++)
            {
                if (words[j].size() == words[i].size() && dbCurrent.find(words[j])!=string::npos)
                    check[j] = true;
            }
            res++;
        }
        }
        return res;
     }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.01.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
​LeetCode刷题实战425:单词方块
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/11/04
2210
​LeetCode刷题实战425:单词方块
​LeetCode刷题实战139:单词拆分
https://leetcode-cn.com/problems/word-break/
程序员小猿
2021/01/19
4040
LeetCode 0068 - Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
Reck Zhang
2021/08/11
2110
【leetcode刷题】T49-键盘行
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
木又AI帮
2019/07/17
4070
【leetcode刷题】T49-键盘行
LeetCode 0318 - Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Reck Zhang
2021/08/11
2240
LeetCode 0030 - Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
Reck Zhang
2021/08/11
1910
【优选算法篇】用滑动窗口解锁 5 大经典问题,轻松应对高频算法题(下篇)
接上篇:【优选算法篇】滑动窗口的艺术:如何动态调整子区间解决复杂问题(中篇)-CSDN博客
熬夜学编程的小王
2024/12/24
1040
【优选算法篇】用滑动窗口解锁 5 大经典问题,轻松应对高频算法题(下篇)
【每日一题】30. Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
公众号-不为谁写的歌
2020/08/02
4720
LeetCode题目30:串联所有单词的子串
给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
二环宇少
2020/08/13
7310
LeetCode题目30:串联所有单词的子串
KWIC-C/C++实现
本文介绍了如何利用C++和Python实现KWIC-C/C++的索引系统,通过分析其实现原理和代码实现,可以加深对KWIC-C/C++的理解,同时也可以学习到如何通过C++和Python实现一些算法题目。
f_zyj
2018/01/09
1.1K0
KWIC-C/C++实现
LeetCode 30. 串联所有单词的子串(字符串哈希)
给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
Michael阿明
2020/07/13
7590
leetcode 30. 串联所有单词的子串----滑动窗口篇八
使用两个哈希表,一个记录words数组中每个字符串出现的次数,一个记录当前滑动窗口中每一个字符串出现的次数。
大忽悠爱学习
2021/11/15
3290
程序员面试金典 - 面试题 17.15. 最长单词(排序+递归)
给定一组单词words,编写一个程序,找出其中的最长单词,且该单词由这组单词中的其他单词组合而成。 若有多个长度相同的结果,返回其中字典序最小的一项,若没有符合要求的单词则返回空字符串。
Michael阿明
2020/07/13
6820
HDU 1247 字典树 拆分单词
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6381    Accepted Submission(s): 2378
csxiaoyao
2019/02/18
5260
LeetCode 1048. 最长字符串链(哈希+DP)
如果我们可以在 word1 的任何地方添加一个字母使其变成 word2,那么我们认为 word1 是 word2 的前身。 例如,“abc” 是 “abac” 的前身。
Michael阿明
2021/02/19
4250
【优选算法】滑动窗口——leetcode——串联所有单词的⼦串(hard)
给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。
小李很执着
2024/08/05
950
【优选算法】滑动窗口——leetcode——串联所有单词的⼦串(hard)
假期算法提升(带你彻底掌握滑动窗口)
呀哈喽,我是结衣。 今天我们要学习的是一种新的算法,也是一种双指针。不过他拥有一个全新的名字”滑动窗口“。
Yui_
2024/10/15
1410
假期算法提升(带你彻底掌握滑动窗口)
LeetCode 318. 最大单词长度乘积(位运算)
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
Michael阿明
2020/07/13
5820
LeetCode 318. 最大单词长度乘积(位运算)
Leetcode: Reverse Words in a String
题目: Given an input string, reverse the string word by word.
卡尔曼和玻尔兹曼谁曼
2019/01/22
3780
Leetcode 68 Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra
triplebee
2018/01/12
6570
相关推荐
​LeetCode刷题实战425:单词方块
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验