【leetcode】1258. Synonymous Sentences
题目如下:
Given a list of pairs of equivalent words
synonyms
and a sentencetext
, Return all possible synonymous sentences sorted lexicographically.Example 1:
Input:
synonyms = [["happy","joy"],["sad","sorrow"],["joy","cheerful"]],
text = "I am happy today but was sad yesterday"
Output:
["I am cheerful today but was sad yesterday",
"I am cheerful today but was sorrow yesterday",
"I am happy today but was sad yesterday",
"I am happy today but was sorrow yesterday",
"I am joy today but was sad yesterday",
"I am joy today but was sorrow yesterday"]Constraints:
0 <= synonyms.length <= 10
synonyms[i].length == 2
synonyms[0] != synonyms[1]
- All words consist of at most
10
English letters only.text
is a single space separated sentence of at most10
words.
解题思路:我的方法是替换,依次遍历synoyms,如果text中存在synoyms[i][0],把text中的第一个synoyms[i][0]替换成synoyms[i][1];同样,如果存在synoyms[i][1],把text中的第一个synoyms[i][1]替换成synoyms[i][0]。然后再对替换过的text做同样的操作,直到不能替换位置。考虑到synoyms[i][0] = 'a',而text中存在'an'这种情况,为了防止误替换,可以把每个单词的前后都加上'#'作为分隔。
代码如下:
class Solution(object):
def generateSentences(self, synonyms, text):
"""
:type synonyms: List[List[str]]
:type text: str
:rtype: List[str]
"""
text = '#' + text.replace(' ', '#') + '#'
queue = [text]
for (w1,w2) in synonyms:
for text in queue :
newtext = text.replace('#' + w1+'#','#' + w2+'#',1)
if newtext != text and newtext not in queue:
queue.append(newtext)
newtext = text.replace('#' + w2 + '#', '#' + w1 + '#',1)
if newtext != text and newtext not in queue:
queue.append(newtext)
res = []
for i in sorted(queue):
newtext = i.replace('#', ' ')
res.append(newtext[1:-1])
return res
【leetcode】1258. Synonymous Sentences的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- 【VS开发】获得devcon.exe
1.获得devcon.exe 有两种方法,一是直接去网上下,不过下的很多64位的都不能用,二是自己装个ddk去安装目录下找,在WinDDK\7600.16385.1\tools\devcon下,当然还 ...
- jmeter性能测试重要指标以及性能结果分析
一.Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告 如果大家都是做Web应用的性能测试,例如只有一个登录的请求,那么在Aggregate Re ...
- 利用docker搭建本地私有镜像仓库
主机名 角色 sht-sgmhadoopcm-01 Docker Repository sht-sgmhadoopnn-01 Docker Client 1. 在两台节点分别安装docker http ...
- [转帖]DotNetCore跨平台~System.DrawingCore部署Linux需要注意的
DotNetCore跨平台~System.DrawingCore部署Linux需要注意的 https://www.bbsmax.com/A/QV5ZemYVJy/?tdsourcetag=s_pc ...
- 设计模式:状态模式(Status)
在介绍状态模式之前,我们先来看这样一个实例:你公司力排万难终于获得某个酒店的系统开发项目,并且最终落到了你的头上.下图是他们系统的主要工作(够简单). 当你第一眼看到这个系统的时候你就看出来了这是一个 ...
- ASP.NET Core中使用EF Core(MySql)Code First
⒈添加依赖 MySql.Data.EntityFrameworkCore ⒉在appsettings.json配置文件中配置数据库连接字符串 { "Logging": { &quo ...
- C++拷贝构造函数:浅拷贝与深拷贝
在介绍C++浅拷贝与深拷贝之前,我们先引出C++的拷贝构造函数. C++拷贝构造函数是一种特殊的构造函数,其形参是本类对象的引用.用于在建立一个新的对象时,使用一个已经存在的对象来初始化这个新对象.因 ...
- 如何用纯 CSS 创作一个精彩的彩虹 loading 特效
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/vjvoow 可交互视频教 ...
- 最短路(hdu2544)
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助 ...
- poj 4005 Moles
大意: 给定$n$元素序列$a$, 依次插入二叉搜索树, 求出$dfs$序列, 对序列每个元素模$2$得到一个长为$2n-1$的$01$序列$s1$. 再给定$01$序列$s2$, 求$s2$在$s1 ...