Java
 Computer >> コンピューター >  >> プログラミング >> Java

Javaで文字ストリームから最初の非反復文字を検索する方法


連続的に入力される文字のストリームの中から、最初に現れた重複しない文字(非反復文字)を検索するには、Javaではリストとboolean型の配列を組み合わせるのが効果的です。以下にその実装例を示します。

サンプルコード

import java.util.ArrayList;
import java.util.List;
public class Demo{
    final static int max_chars = 256;
    static void non_repeating_char(){
        List<Character> my_list = new ArrayList<Character>();
        boolean[] repeat = new boolean[max_chars];
        String my_str = "Thisisasample";
        for (int i = 0; i < my_str.length(); i++){
            char x = my_str.charAt(i);
            if (!repeat[x]){
                if (!(my_list.contains(x))){
                    my_list.add(x);
                }
                else{
                    my_list.remove((Character)x);
                    repeat[x] = true;
                }
            }
            if (my_list.size() != 0){
                System.out.print("The first non-repeating character of the string is ");
                System.out.println(my_list.get(0));
            }
        }
    }
    public static void main(String[] args){
        non_repeating_char();
    }
}

実行結果

The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T
The first non-repeating character of the string is T

プログラムの仕組み

このプログラムでは、Demo というクラス内に non_repeating_char というメソッドを定義しています。処理の流れは以下のとおりです。

まず、出現中の文字を管理するための List<Character> を作成し、対象となる文字列(ここでは「Thisisasample」)を定義します。次に、文字列を先頭から1文字ずつ走査し、各文字がすでに繰り返し出現したかどうかを repeat というboolean型の配列に記録します。繰り返しが確認された文字には true が設定され、まだ一度しか出現していない文字は false のまま保持されます。

具体的には、ある文字が初めて登場した場合はリストに追加され、2回目以降に登場した場合はリストから削除されると同時に repeat 配列にフラグが立てられます。これにより、リストの先頭要素(インデックス0)が常に「現在時点で最初の非反復文字」を表すことになります。

最後に、main メソッドからこのメソッドを呼び出すことで処理が実行され、各ステップにおける最初の非反復文字がコンソールに出力されます。この例では、文字「T」が他のどの文字とも重複していないため、常に「T」が結果として表示されます。

  1. 【Python】文字ストリームから最初に一度だけ現れる文字を検索する方法

    文字ストリーム(あるいは単純な文字列)が与えられ、その中から最初に一度だけ出現する文字を見つける問題を考えてみましょう。例えば、文字列が「people」の場合、出現回数が1回となる最初の文字は「o」であり、そのインデックス 2 を返します。該当する文字が存在しない場合は -1 を返します。 解法のアプローチ この問題は、各文字の出現回数を記録する「頻度マップ(ハッシュマップ)」を使うことで効率的に解けます。手順は以下のとおりです。 空の頻度マップ(辞書)を作成する 文字列内の各文字 c について次の処理を行う c がマップに存在しない場合は、キー c を値 1 で登録する すでに存在する場

  2. 【Python入門】文字列の中から最初の繰り返しのない文字を見つける2つの方法

    この記事では、文字列や文字のストリームの中から最初に現れる繰り返しのない文字(ユニークな文字)を見つける方法を解説します。この問題には複数のアプローチがあり、本稿では同じ文字列に対して2つの異なるプログラムを作成して比較してみます。方法1:関数と辞書を使う方法(O(n)アルゴリズム)まずは、辞書(dict)を使って各文字の出現回数をカウントし、出現順序も保持する効率的な関数ベースの方法です。def firstNonRepeatingChar(str1): char_order = [] counts = {} for c in str1: if c in