Google Tech Dev Guideのコーディング問題解いてみた〜その4(ついでに日本語訳も)

目次

  1. Without String問題
  2. 回答
  3. 再帰を使う
  4. replaceAll関数を使う
  5. まとめ
  6. 関連記事
  7. PR

こんにちは。 かりんとうマニア(@karintozuki)です。 

この記事は、Googleが提供するコーディング教材Google Tech Dev Guideの解説をする記事の第四弾です。
こちらから関連記事がまとめて見られます。
Google Tech Dev Guideのコーディング問題解いてみた 記事一覧

Without String問題

今回の問題はWithout Stringと名付けられた問題です。

問題はこちらです。(元のサイトはこちら:https://codingbat.com/prob/p192570

Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed (not case sensitive).
You may assume that the remove string is length 1 or more. Remove only non-overlapping instances, so with “xxx” removing “xx” leaves “x”.

和訳です。

base, removeの二つの文字列が与えられたときにbaseから
全てのremove文字列を取り除いた文字列を返す関数をかえすwithoutString関数を作成してください。
ケースは無視します。(大文字小文字を区別しない。)
removeは一文字以上の文字列です。
重複するぶんは除去しません。”xxx”から”xx”を除去した結果は”x”です。

この関数は以下のような動きをします。

withoutString(“Hello there”, “llo”) → “He there”
withoutString(“Hello there”, “e”) → “Hllo thr”
withoutString(“Hello there”, “x”) → “Hello there”

回答

substringとindexOf関数を上手い感じに使うと出来そうです。

WithoutString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
String result = base;

//base,removeを小文字に直す
String baseLower = base.toLowerCase();
String removeLower = remove.toLowerCase();

int removeStartIndex = 0;

// removeがbaseLowerから見つからなくなるまでループ
while((removeStartIndex = baseLower.indexOf(removeLower)) != -1){

// baseLower, resultからremoveを削除
baseLower = baseLower.substring(0, removeStartIndex) + baseLower.substring(removeStartIndex + remove.length(), baseLower.length());
result = result.substring(0, removeStartIndex) + result.substring(removeStartIndex + remove.length(), result.length());

}

return result;

再帰を使う

再帰を使うことも可能です。
理屈としては先ほどのコードと同じですが、
whileの代わりに関数内で自信を呼び出すような再起処理になっています。

removeした後のBase文字列にまだremove対象が含まれていれば
関数内から自分自身(withoutString関数)を呼び出します。

WithoutString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
public static String withoutString(String base, String remove) {
int removeIndex = base.toLowerCase().indexOf(remove.toLowerCase());

if(removeIndex == -1){
// removeが見つからなければbaseか返す
return base;
}else{
// removeが見つかったら除去してもう一度処理
base = base.substring(0, removeIndex) + base.substring(removeIndex + remove.length(), base.length());
return withoutString(base, remove);
}

}

こちらの方がスッキリしますが、やや理解が難しいかもしれません。

replaceAll関数を使う

一応出題者の意図に沿うためみてみぬふりをしていたのですが、
JavaのStringクラスには元々replaceAllという
withoutStringと同じ挙動をする関数が用意されています爆

ただ、この問題のミソとしてケースを無視する、というのがあるかと思いますが、
これは正規表現に(?i)を使うことで実現します。
ignore caseのiですね。

WithoutString.java
1
2
3
public static String withoutString(String base, String remove) {
return base.replaceAll("(?i)"+remove, "");
}

うーん、コーディング面接でこの解答をするかはお任せしますが、
業務で使う際はこちらが正解ですね。

まとめ

文字列操作は定番の出題なので、
難なくループやSubstringを使いこなせると良いですね。

それじゃ今日はこの辺で。

関連記事

コーディング問題の関連記事一覧です。
Google Tech Dev Guideのコーディング問題解いてみた 記事一覧

PR

あなたの会社はあなたの技術を評価してくれていますか?
技術力を高めようと頑張っているのに、
技術が評価されないような会社にいたらそれは良い環境なのでしょうか?
エンジニアとして技術を高めたいのなら環境を選ぶことも大事です。

レバテックキャリア
エンジニアとして働いていて実務経験があるなら、
求人数の充実具合からレバテックキャリアがおすすめです。
IT転職ではデファクト・スタンダードですね。
▼レバテック キャリア 登録はこちら▼


Tech Clips
Tech Clipsは年収500万以上&自社サービスを持った会社に特化した求人サイトです。
首都圏限定になってはしまいますが、
収入を増やしたい、自社サービスを持った企業への転職をしたい人におすすめです。

▼Tech Clips 登録はこちら▼