0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

学習記録(Bランク:文字列3)

Posted at

今回の問題

入力文字列から、指定文字列の完全一致かleet一致(a→@のような揺らぎ)の部分を見つける問題。
当初はsubstringで一文字ずつ切り出して判定するロジックを考えましたが、めちゃめちゃな入れ子になりそうだったため断念。
leetの組み合わせは54通りと少ないので、あらかじめ洗い出してから探し出すロジックにしました。

私のコード

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        //leet候補をすべて洗い出す
        List<String>leets = new ArrayList<String>();
        String[]a = {"a","4","@"};
        String[]i = {"i","1","!"};
        String[]z = {"z","2"};
            for(int j = 0;j < a.length;j++){
                for(int x = 0; x < i.length;x++){
                    for(int y = 0; y < z.length; y++){
                        for(int zz = 0;zz < a.length;zz++){
                            leets.add("p" + a[j] + i[x] + z[y] + a[zz]);
                        }
                    }
                }
            }

        if(str.contains("paiza")){
        //文字列に[paiza]が含まれる場合
            System.out.println("paiza");
            return;
        }else{
            for(int ii = 0; ii < leets.size();ii++){
            //文字列にleetが含まれる場合
                if(str.contains(leets.get(ii))){
                    System.out.println("leet");
                    return;
                }
            }
        }
        //上記に該当しなかった場合
        System.out.println("nothing");
    }
}

おわりに

このコードでも問題なくクリアしましたが、正答例を見たところもっと凝ったソースになっていました、、
私的には、ある程度冗長でもひとつひとつの処理がわかりやすいコードのほうが好みなんですが、実際どちらが良いんでしょう?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?