【leetcode】1258. Synonymous Sentences
题目如下:
Given a list of pairs of equivalent words
synonymsand 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 <= 10synonyms[i].length == 2synonyms[0] != synonyms[1]- All words consist of at most
10English letters only.textis a single space separated sentence of at most10words.
解题思路:我的方法是替换,依次遍历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 ...
随机推荐
- 【Linux开发】linux设备驱动归纳总结(七):2.内核定时器
linux设备驱动归纳总结(七):2.内核定时器 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- python 爬虫 urllib模块介绍
一.urllib库 概念:urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urll ...
- ZooKeeper原理及介绍
Zookeeper简介 1.1 什么是Zookeeper ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是大数据生态中的重要组件.它是 ...
- C++练习 | 模板与泛式编程练习(2)
#include <iostream> #include <cmath> #include <cstring> #include <string> #i ...
- linux_文本编译使用命令
一:字符模式与shell命令 字符界面和图形界面 字符界面优点: 1):系统执行效率高,稳定性高,执行结果可直接返回 2):节省系统资源,对一个服务器至关重要 3):节省大量网络开销,大幅降低运行成本 ...
- spark教程(13)-shuffle介绍
shuffle 简介 shuffle 描述了数据从 map task 输出到 reduce task 输入的过程,shuffle 是连接 map 和 reduce 的桥梁: shuffle 性能的高低 ...
- 如何不用 transition 和 animation 也能做网页动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/BxbQJj 可交互视频教 ...
- Linux-1.4文件操作命令(grep,cat,tail,head,less,find,chmod,tail,less)
Linux基础命令(grep,cat,tail,head,less,find,chmod,tail,less) grep(常用) grep 指定“文件”搜索文件内容 grep hello 1.txt ...
- Linux-1.2关机重启reboot,shutdown
关机重启:reboot,shutdown reboot 重启操作系统 shutdown -r now 重启,shutdown会给其他用户提示
- 在Windows下安装BIND作为DNS服务器(模拟网站比较有用)
本文参考了CU下的一篇帖子,感谢:) 1.下载BIND http://ftp.isc.org/isc/bind9/9.4.3/BIND9.4.3.zip 2.安装 下载回来是zip的压缩包,解压 ...