公司如何建站,萧山区seo关键词排名,旅游网站首页设计模板,自己做的网站怎么放图片题目描述#xff1a; 给你两个字符串 start 和 target #xff0c;长度均为 n 。每个字符串 仅 由字符 ‘L’、‘R’ 和 ‘_’ 组成#xff0c;其中#xff1a; 字符 ‘L’ 和 ‘R’ 表示片段#xff0c;其中片段 ‘L’ 只有在其左侧直接存在一个 空位 时才能向 左 移动 给你两个字符串 start 和 target 长度均为 n 。每个字符串 仅 由字符 ‘L’、‘R’ 和 ‘_’ 组成其中 字符 ‘L’ 和 ‘R’ 表示片段其中片段 ‘L’ 只有在其左侧直接存在一个 空位 时才能向 左 移动而片段 ‘R’ 只有在其右侧直接存在一个 空位 时才能向 右 移动。 字符 ‘__’ 表示可以被 任意 ‘L’ 或 ‘R’ 片段占据的空位。 如果在移动字符串 start 中的片段任意次之后可以得到字符串 target 返回 true 否则返回 false 。 示例 解题思路 由于通过字符个数和顺序排除一部分在通过判断每个“L”字符能不能往前移每个“R”字符能不能往后移。 相关代码
class Solution {public boolean canChange(String start, String target) {if(!start.replace(_,).equals(target.replace(_,))) {return false;}int[] snew int[start.replace(_,).length()];int[] tnew int[target.replace(_,).length()];index(start,s);index(target,t);for(int i0;is.length;i) {if(start.charAt(s[i])Ls[i]t[i]||start.charAt(s[i])Rs[i]t[i]) return false;}return true;}public void index(String x,int[] n) {int j0;for(int i0;ix.length();i) {if(x.charAt(i)!_) {n[j]i;j;}}}
}代码效率