結果

問題 No.927 Second Permutation
ユーザー Theta
提出日時 2022-12-20 11:20:51
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 664 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 14,320 KB
最終ジャッジ日時 2024-11-18 01:44:52
合計ジャッジ時間 2,453 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter


def main():
    X = input()
    counter_X = Counter(X)
    if counter_X["0"] == len(X) - 1:
        print(-1)
        return
    if len(counter_X) == 1:
        print(-1)
        return

    largest_X = "".join(sorted(list(X), key=int, reverse=True))

    swap_idx = -1
    while abs(swap_idx) < len(X):
        if largest_X[swap_idx] != largest_X[swap_idx-1]:
            break
        swap_idx -= 1

    second_large = list(largest_X)
    second_large[swap_idx], second_large[swap_idx-1] = (
        second_large[swap_idx-1], second_large[swap_idx]
    )
    print("".join(second_large))


if __name__ == "__main__":
    main()
0