【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 ...
随机推荐
- 【并行计算-CUDA开发】OpenCL、OpenGL和DirectX三者的区别
什么是OpenCL? OpenCL全称Open Computing Language,是第一个面向异构系统通用目的并行编程的开放式.免费标准,也是一个统一的编程环境,便于软件开发人员为高性能计算服务器 ...
- mint ui解决Navbar和Infinite scroll共存时的bug
Navbar和Infinite scroll共同使用时会出现无限加载的问题,滑动也会出现乱加载. 只需要判断一下就可以了,代码: html: <mt-navbar v-model="s ...
- [转帖]【JDK和Open JDK】平常使用的JDK和Open JDK有什么区别
https://www.cnblogs.com/sxdcgaq8080/p/7487369.html 其实不同的 openjdk的版本也不一样. atlassian说AdoptOpenJDK我们测试充 ...
- zabbix-3.4.6安装
先安装myql和phpmysql5.7.17: http://www.cnblogs.com/cjsblogs/p/8116782.htmlphp7.2.1: http://www.cnblogs.c ...
- CentOS7 安装ffmpeg
安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持:yum install -y epel-release#如果出现缺少Code提示,可以: sudo rpm --i ...
- leecode刷题(31) -- 回文数
leecode刷题(31) -- 回文数 回文数 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输 ...
- DES加密解密 MD5加密解密
#region MD5 加密 /// <summary> /// MD5加密静态方法 /// </summary> /// <param name="Encry ...
- Spring IOC的简单实现
简单的说,Spring就是通过工厂+反射将我们的bean放到它的容器中的,当我们想用某个bean的时候,只需要调用getBean("beanID")方法即可. 原理简单说明: Sp ...
- dedecms body 输出自动替换结果。
{dede:field.body function='str_replace("张三","李四",@me)'/} body内容中的张三全部换成了李四
- centos 7 Network 脚本
#!/bin/sh #主动启动网卡 interface=$() ifup $interface #获取当前网络信息 default_route=$(ip route show) default_int ...