【Codewars】Dubstep

24 篇文章 0 订阅
4 篇文章 0 订阅

Codewars里的 6kyu Kata。

题目说明:

Description:

Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.

Let's assume that a song consists of some number of words (that don't contain WUB). To make the dubstep remix of this song, Polycarpus inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.

For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".

Recently, Jonny has heard Polycarpus's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Polycarpus remixed. Help Jonny restore the original song.

Input

The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200 characters

Output

Return the words of the initial song that Polycarpus used to make a dubsteb remix. Separate the words with a space.

Examples

songDecoder("WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB")
  // =>  WE ARE THE CHAMPIONS MY FRIEND

解题代码:

public class Dubstep {
    public static String SongDecoder (String song){
        // Your code is here...
        System.out.println(song);
        StringBuilder sb = new StringBuilder();
        int i, j;
        for(i = 0; i<song.length()-2; i++){
            System.out.println("pre:" + sb);
            if(song.substring(i, i+3).equals("WUB")){
                sb.append(" ");
                i += 2;
            }else{
                sb.append(song.substring(i, i+1));
            }
            System.out.println("mod:" + sb);
        }
        
        if(i < song.length()) {
            sb.append(song.substring(i, song.length()));
        }
        
        while(sb.substring(0, 1).equals(" ")){
            sb.delete(0, 1);
        }
        
        while(sb.substring(sb.length()-1, sb.length()).equals(" ")){
            sb.delete(sb.length()-1, sb.length());
        }
        
        i = 0; j = 0;
        while(i < sb.length()){
            if(sb.charAt(i) != ' '){
                i++;
            }else{
                j = i + 1;
                while(j < sb.length()){
                    if(sb.charAt(j) == ' '){
                        j++;
                    }else{
                        sb.replace(i, j, " ");
                        i++;
                        break;
                    }
                }
            }
        }
        
        return sb.toString();
    }
}

Test Cases:

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SongTests {
    @Test
    public void Test1() {
        assertEquals("ABC", new Dubstep().SongDecoder("WUBWUBABCWUB"));
    }

    @Test
    public void Test2() {
        assertEquals("R L", new Dubstep().SongDecoder("RWUBWUBWUBLWUB"));
    }

    @Test
    public void Test3() {
        assertEquals("JKD WBIRAQKF YE WV", new Dubstep().SongDecoder("WUBJKDWUBWUBWBIRAQKFWUBWUBYEWUBWUBWUBWVWUBWUB"));
    }

    @Test
    public void Test4() {
        assertEquals("KSDHEMIXUJ R S H",
                new Dubstep().SongDecoder("WUBKSDHEMIXUJWUBWUBRWUBWUBWUBSWUBWUBWUBHWUBWUBWUB"));
    }

    @Test
    public void Test5() {
        assertEquals("Q QQ I WW JOPJPBRH", new Dubstep().SongDecoder("QWUBQQWUBWUBWUBIWUBWUBWWWUBWUBWUBJOPJPBRH"));
    }

    @Test
    public void Test6() {
        assertEquals("O IPVCQAFWY Q XHDKCPYKCTWWY V FZ", new Dubstep()
                .SongDecoder("WUBWUBOWUBWUBWUBIPVCQAFWYWUBWUBWUBQWUBWUBWUBXHDKCPYKCTWWYWUBWUBWUBVWUBWUBWUBFZWUBWUB"));
    }

    @Test
    public void Test7() {
        assertEquals("YYRTSMNWU C C FSYUINDWOBV F AU V JB", new Dubstep().SongDecoder(
                "WUBYYRTSMNWUWUBWUBWUBCWUBWUBWUBCWUBWUBWUBFSYUINDWOBVWUBWUBWUBFWUBWUBWUBAUWUBWUBWUBVWUBWUBWUBJB"));
    }

    @Test
    public void Test8() {
        assertEquals("KSDHEMIXUJ R S H",
                new Dubstep().SongDecoder("WUBKSDHEMIXUJWUBWUBRWUBWUBWUBSWUBWUBWUBHWUBWUBWUB"));
    }

    @Test
    public void Test9() {
        assertEquals("A", new Dubstep().SongDecoder("AWUBWUBWUB"));
    }

    @Test
    public void Test10() {
        assertEquals("A B C D", new Dubstep().SongDecoder("AWUBBWUBCWUBD"));
    }

    @Test
    public void Test11() {
        assertEquals("W U B", new Dubstep().SongDecoder("WUBWWUBWUBWUBUWUBWUBBWUB"));
    }

    @Test
    public void Test12() {
        assertEquals("WU BW UB", new Dubstep().SongDecoder("WUWUBBWWUBUB"));
    }

    @Test
    public void Test13() {
        assertEquals("WUAB", new Dubstep().SongDecoder(
                "WUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUABWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUBWUB"));
    }

    @Test
    public void Test14() {
        assertEquals("U", new Dubstep().SongDecoder("U"));
    }

    @Test
    public void Test15() {
        assertEquals("WU", new Dubstep().SongDecoder("WUWUB"));
    }

    @Test
    public void Test16() {
        assertEquals("UB", new Dubstep().SongDecoder("UBWUB"));
    }

    @Test
    public void Test17() {
        assertEquals("WU UB U", new Dubstep().SongDecoder("WUWUBUBWUBUWUB"));
    }

    @Test
    public void Test18() {
        assertEquals("W A", new Dubstep().SongDecoder("WUBWWUBAWUB"));
    }

    @Test
    public void Test19() {
        assertEquals("WUUUUU", new Dubstep().SongDecoder("WUUUUU"));
    }

    @Test
    public void Test20() {
        assertEquals("A", new Dubstep().SongDecoder("WUBWUBA"));
    }
}

个人总结:

public class Dubstep {
    public static String SongDecoder(String song) {
        return song.replaceAll("(WUB)+", " ").trim();
    }
}
public class Dubstep {
    public static String SongDecoder(String song) {
        song = song.replaceAll("WUB", " ");
        return song.trim().replaceAll(" +", " ");
    }
}
import java.util.Arrays;
import java.util.stream.Collectors;

public class Dubstep {
    public static String SongDecoder(String song) {
        return Arrays.stream(song.split("WUB")).filter(i -> !"".equals(i)).collect(Collectors.joining(" "));
    }
}
public class Dubstep {
    public static String SongDecoder(String song) {
        // error handle
        if (song.length() > 200 || song.isEmpty()) {
            return "";
        }

        // is it possible to do this with regular expressions too?

        // anyways... start to walk the string as a char array, we are going to try for
        // < O(n) time
        char[] songCharArr = song.toCharArray();
        StringBuilder strBuilder = new StringBuilder();
        boolean buildingWord = false; // helps figure out to put a single space between words

        int i = 0;
        while (i < song.length()) {

            // stop checking before our 'lookahead' goes out of bounds
            // ex. WUB has a length of 3 so this is fine too look for WUB
            // ^ + 2 = B
            // but WUB would go out of bounds
            // ^ + 2 = out of bounds, hence minus 2 from the length
            if ((i < song.length() - 2) && songCharArr[i] == 'W' && songCharArr[i + 1] == 'U'
                    && songCharArr[i + 2] == 'B') {

                if (buildingWord) {
                    strBuilder.append(" ");
                    buildingWord = false;
                }

                i += 3;

            } else {
                strBuilder.append(songCharArr[i]);
                buildingWord = true;
                i += 1;
            }
        }

        return strBuilder.toString().trim();

    }
}
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Dubstep {
    public static String SongDecoder(String song) {
        return Stream.of(song.split("WUB")).filter(s -> !s.isEmpty()).collect(Collectors.joining(" "));
    }
}
public class Dubstep {
    public static String SongDecoder(String song) {
        return song.replace("WUB", " ").trim().replaceAll("\\s+", " ");
    }
}
public class Dubstep {
    public static String SongDecoder(String song) {
        song = song.replaceAll("WUB", " ");
        return song.replaceAll("\\s+", " ").trim();
    }
}

学会调试真的很重要!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值