Title: 777.在lr字符串中交换相邻字符 · Issue #7 · feikerwu/algorithm-camp · GitHub
Open Graph Title: 777.在lr字符串中交换相邻字符 · Issue #7 · feikerwu/algorithm-camp
X Title: 777.在lr字符串中交换相邻字符 · Issue #7 · feikerwu/algorithm-camp
Description: 原题地址 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换 一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动 操作使得start可以转换成end时, 返回True。 题解 题目只需要求从start能否通过 'XL' => 'LX', 'RX' => 'XR' 操作到end, 观察到'XL'只能替换成...
Open Graph Description: 原题地址 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换 一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动 操作使得start可以转换成end时, 返回True。 题解 题目只需要求从start能否通过 'XL' =>...
X Description: 原题地址 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换 一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end...
Opengraph URL: https://github.com/feikerwu/algorithm-camp/issues/7
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"777.在lr字符串中交换相邻字符","articleBody":"[原题地址](https://leetcode-cn.com/problems/swap-adjacent-in-lr-string/)\r\n\r\n\u003e 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如\"RXXLRXRXL\")中进行移动操作。一次移动操作指用一个\"LX\"替换 一个\"XL\",或者用一个\"XR\"替换一个\"RX\"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动 操作使得start可以转换成end时, 返回True。\r\n\r\n### 题解\r\n题目只需要求从start能否通过 'XL' =\u003e 'LX', 'RX' =\u003e 'XR' 操作到end, 观察到'XL'只能替换成'LX', 'RX'只能替换成'XR', 也就是'L'只能向左移动,'R'只能向右移动,且'L'和'R'不能交换。\r\n所以要判断start 能否到达end,只需要满足以下两个条件即可\r\n1. start去掉'X'后的字符串 等于 end去掉'X'后的字符串\r\n2. 如果满足1,那么对每个满足start[p1] === end[p2] 的位置\r\n 1. 如果start[p1] === 'L', 那么 p1 必须 \u003e= p2, 因为 L 只能向左移动\r\n 2. 同理,如果end[p1] === 'R', 那么 p1 必须 \u003c= p2, 因为 R 只能向右移动\r\n\r\n### 代码实现\r\n```js\r\n/**\r\n * @param {string} start\r\n * @param {string} en\r\n * @return {boolean}\r\n */\r\nvar canTransform = function(start, end) {\r\n let startCopy = start\r\n let endCopy = end\r\n if (startCopy.replace(/X/g, '') !== endCopy.replace(/X/g, '')) {\r\n console.log('here')\r\n return false\r\n }\r\n let p1 = p2 = 0;\r\n while(p1 \u003c start.length \u0026\u0026 p2 \u003c end.length) {\r\n while(start[p1] === 'X') {\r\n p1++\r\n }\r\n while(end[p2] === 'X') {\r\n p2++\r\n }\r\n if (start[p1] !== end[p2]) {\r\n return false\r\n } else {\r\n if ((start[p1] === 'L' \u0026\u0026 p1 \u003c p2) || (start[p1] === 'R' \u0026\u0026 p1 \u003e p2)) {\r\n return false\r\n }\r\n }\r\n p1++;\r\n p2++;\r\n }\r\n return true\r\n};\r\n```","author":{"url":"https://github.com/feikerwu","@type":"Person","name":"feikerwu"},"datePublished":"2020-03-04T09:19:19.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/7/algorithm-camp/issues/7"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0523837f-74a7-8c8d-81fc-a0a7ba5bc520 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE1C:360EE8:310357F:419384E:697AED07 |
| html-safe-nonce | 8dcfc9fbd11355f8f334db82e2d5e4ab7c823a3ec20df9b4f00704c9450e30db |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTFDOjM2MEVFODozMTAzNTdGOjQxOTM4NEU6Njk3QUVEMDciLCJ2aXNpdG9yX2lkIjoiMzIwMzcxMDI2OTQ2NjE2NTgzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c8ee60687ec12173b43194c33e1daf205905b5498ab80d4ab8ab36ca7ced379f |
| hovercard-subject-tag | issue:575248430 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/feikerwu/algorithm-camp/7/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b9556036ebb7e718d029196976ea348a0c23a72ed9dd8c316f92a3605bfe71ba/feikerwu/algorithm-camp/issues/7 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b9556036ebb7e718d029196976ea348a0c23a72ed9dd8c316f92a3605bfe71ba/feikerwu/algorithm-camp/issues/7 |
| og:image:alt | 原题地址 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换 一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动 操作使得start可以转换成end时, 返回True。 题解 题目只需要求从start能否通过 'XL' =>... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | feikerwu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7ce8ed0a54c4730aeca4e6abacfc6490365fc42e25480b86883054df3f9181c8 |
| turbo-cache-control | no-preview |
| go-import | github.com/feikerwu/algorithm-camp git https://github.com/feikerwu/algorithm-camp.git |
| octolytics-dimension-user_id | 39146693 |
| octolytics-dimension-user_login | feikerwu |
| octolytics-dimension-repository_id | 242958294 |
| octolytics-dimension-repository_nwo | feikerwu/algorithm-camp |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 242958294 |
| octolytics-dimension-repository_network_root_nwo | feikerwu/algorithm-camp |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d7bfc78137af9a4828305e52ab993fce981d7085 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width