[Algo] 649. String Replace (basic)
2024-09-07 04:22:18
Given an original string input, and two strings S and T, replace all occurrences of S in input with T.
Assumptions
- input, S and T are not null, S is not empty string
Examples
- input = "appledogapple", S = "apple", T = "cat", input becomes "catdogcat"
- input = "laicode", S = "code", T = "offer", input becomes "laioffer"
public class Solution {
public String replace(String input, String source, String target) {
// Write your solution here
StringBuilder sb = new StringBuilder();
int start = 0;
int match = input.indexOf(source, start);
while (match != -1) {
// append end index exclusive
sb.append(input, start, match).append(target);
start = match + source.length();
match = input.indexOf(source, start);
}
sb.append(input, start, input.length());
return sb.toString();
}
}
最新文章
- 转:CentOS/Debian/Ubuntu一键安装LAMP(Apache/MySQL/PHP)环境
- ctf汇总
- iOS地图
- 命令与文件的查询 which whereis locate find
- Python 5 —— OOP
- CSS 高级:尺寸、分类、伪类、伪元素
- PrintWriter的print和write方法(转)
- Js遍历Josn对象(内容对比页实现思路)
- PHP文件目录copy
- matlab三维画图
- spring mvc后台接收中文乱码
- Web前端学习(1):上网的过程与网页的本质
- Spring MVC Restful Put方法无法获取参数值
- [codeforces 804F. Fake bullions]
- struts2-剩余
- django介绍及路由系统
- View的getMeasuredWidth和getWidth有什么区别?
- HDU4185 Oil Skimming 二分图匹配 匈牙利算法
- Hibernate(1)基本知识
- 【java多线程】队列系统之ArrayBlockingQueue源码