Day63:字符串转换

198 阅读1分钟

Day12 2023/03/11

难度:简单

题目

编写一个程序实现将字符串中的所有"you"替换成"we"

  • 输入:包含多行数据每行数据是一个字符串,长度不超过1000,数据以EOF结束
  • 输出:对于输入的每一行,输出替换后的字符串

示例1

输入:you love it!
输出:we love it!
说明:将you替换为了we
static void change(String word){
    char txt[] = word.toCharArray();
   for (int i = 0;i < word.length();i++){
       if (txt[i] == 'y' && txt[i+1] == 'o' && txt[i+2] == 'u'){
           System.out.print("we");
           i+=2;
       }
     else System.out.print(txt[i]);
   }
}
  • 时间复杂度 O(n)--- 除去输入数据占用的时间外,仅遍历一组字符串,其中n为字符串长度
  • 空间复杂度 O(1)--- 仅常数级变量,无额外的辅助空间