Làm thế nào để bạn tìm thấy các ký tự không lặp lại trong một chuỗi trong python?
Mã nguồnstr1="Tutor Joes" char_order = [] ctr = {} for c in str1: if c in ctr: ctr[c] += 1 else: ctr[c] = 1 char_order.append(c) for c in char_order: if ctr[c] == 1: print(c) đầu raT u t r J e s Để tải file raw Bấm vào đây Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên Show
Trình chỉnh sửa mã Python Có một cách khác để giải quyết giải pháp này? Trước. Viết chương trình Python để tách một chuỗi ở lần xuất hiện cuối cùng của dấu phân cách Mức độ khó của bài tập này là gì? Dễ dàng trung bình khóKiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource Theo dõi chúng tôi trên Facebook và Twitter để cập nhật thông tin mới nhất. con trăn. Lời khuyên trong ngàyDeque Một deque hoặc (Hàng đợi kết thúc kép) là một đối tượng Python hai đầu mà bạn có thể thực hiện một số thao tác nhất định từ cả hai đầu Nó có thể được sử dụng khi cần vận hành một bộ sưu tập ở cả hai đầu và có thể mang lại hiệu quả và sự đơn giản so với các cấu trúc dữ liệu truyền thống như danh sách Trong hướng dẫn này, chúng ta sẽ học viết chương trình python để in tất cả các ký tự của chuỗi không lặp lại. Có thể nói ký tự chỉ có một lần trong chuỗi Báo cáo vấn đềLấy bất kỳ chuỗi nào làm đầu vào từ người dùng và kiểm tra xem có ký tự nào trong chuỗi lặp lại hay không. nếu nó không lặp lại thì in ký tự đó dưới dạng đầu ra Ví dụ Trường hợp 1. Nếu người dùng nhập chuỗi 'lập trình trăn' Sau đó, đầu ra phải là 'ythai', trong đó không có ký tự nào lặp lại Trường hợp2. Nếu người dùng nhập chuỗi 'quescoll' Sau đó, đầu ra phải là 'quesco', trong đó không có ký tự nào lặp lại [phần tử-mẫu id =”5257″] Logic của chúng tôi để tìm tất cả các ký tự không lặp lại trong chuỗi
Thuật toán tìm tất cả các ký tự không lặp lại trong chuỗiBước 1. Bắt đầu [phần tử-mẫu id =”5253″] Mã Python để tìm tất cả các ký tự không lặp lại trong chuỗiđầu ra Đầu vào. "thuật toán" Đề xuất thực hành Ký tự không lặp lạiHãy thử Cách tiếp cận ngây thơ
Ghi chú. Trong python để tìm sự xuất hiện của một ký tự trong chuỗi có In-Built Function string. đếm() Dưới đây là việc thực hiện các phương pháp trên Python3
First non-repeating character is f0 = First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f4_______210_______ First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f8 First non-repeating character is f9 First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f2 First non-repeating character is f3 = = First non-repeating character is f3 First non-repeating character is f7 First non-repeating character is f8 First non-repeating character is f4 First non-repeating character is f0 = First non-repeating character is f8 First non-repeating character is f8 First non-repeating character is f4 First non-repeating character is f1 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f8 First non-repeating character is f0_______2_______0 = First non-repeating character is f3 First non-repeating character is f2 First non-repeating character is f0 = = First non-repeating character is f3 First non-repeating character is f7 First non-repeating character is f1 First non-repeating character is f0_______2_______1 First non-repeating character is f2_______2_______3 First non-repeating character is f6 First non-repeating character is f7 First non-repeating character is f1 First non-repeating character is f0_______2_______1 First non-repeating character is f9 # Python program to print the first non-repeating character 0Đầu ra First non-repeating character is f Thời gian phức tạp. O(N2), Duyệt qua chuỗi cho mọi ký tự trong chuỗi có kích thước N Ký tự không lặp lại đầu tiên sử dụng hàm chuỗi find()
Việc tìm kiếm được thực hiện bằng hàm find() tích hợp Dưới đây là việc thực hiện phương pháp C++
First non-repeating character is f1 First non-repeating character is f7 First non-repeating character is f1 string 2 string 3
First non-repeating character is f8 First non-repeating character is f2 string 6
First non-repeating character is f8 = 8First non-repeating character is f1 = 8First non-repeating character is f1 = 0"geeksforgeeks" 3
First non-repeating character is f1 = 5= 6
First non-repeating character is f01
First non-repeating character is f03
First non-repeating character is f1 First non-repeating character is f06 "geeksforgeeks" = 6First non-repeating character is f1 First non-repeating character is f10
First non-repeating character is f12 JavaFirst non-repeating character is f13 First non-repeating character is f14 First non-repeating character is f15
First non-repeating character is f16 First non-repeating character is f17
First non-repeating character is f18 First non-repeating character is f19 First non-repeating character is f18 First non-repeating character is f21 First non-repeating character is f18 First non-repeating character is f23 First non-repeating character is f24 # Python program to print the first non-repeating character 6 First non-repeating character is f26 First non-repeating character is f18 # Python program to print the first non-repeating character 8
First non-repeating character is f1 First non-repeating character is f7 First non-repeating character is f1 string 2 First non-repeating character is f33 First non-repeating character is f34 First non-repeating character is f35
First non-repeating character is f36_______2_______2 First non-repeating character is f38 First non-repeating character is f3_______2_______40 First non-repeating character is f3_______2_______42 First non-repeating character is f8 First non-repeating character is f44 = 1First non-repeating character is f46 First non-repeating character is f8 First non-repeating character is f4 = 6First non-repeating character is f36_______210_______8 First non-repeating character is f1 = 8First non-repeating character is f1 = 5= 6First non-repeating character is f18 = 8First non-repeating character is f18 First non-repeating character is f23 First non-repeating character is f24 # Python program to print the first non-repeating character 6 First non-repeating character is f63 First non-repeating character is f1 First non-repeating character is f65 "geeksforgeeks" = 6First non-repeating character is f1 First non-repeating character is f10 First non-repeating character is f18 = 8
First non-repeating character is f73 Python3First non-repeating character is f74
First non-repeating character is f75 First non-repeating character is f76
First non-repeating character is f1 First non-repeating character is f7 First non-repeating character is f8 First non-repeating character is f9 First non-repeating character is f81
First non-repeating character is f8 First non-repeating character is f2 First non-repeating character is f84_______2_______0 First non-repeating character is f3 First non-repeating character is f87 = 210_______ First non-repeating character is f2 First non-repeating character is f3 First non-repeating character is f7
First non-repeating character is f0 First non-repeating character is f1 First non-repeating character is f9 First non-repeating character is f97
First non-repeating character is f4
First non-repeating character is f1____210_______5
First non-repeating character is f02
First non-repeating character is f03____210_______ First non-repeating character is f05
First non-repeating character is f06 C#First non-repeating character is f07 First non-repeating character is f08
First non-repeating character is f10
First non-repeating character is f23 First non-repeating character is f24 First non-repeating character is f16 First non-repeating character is f17
First non-repeating character is f1 First non-repeating character is f19 First non-repeating character is f1 First non-repeating character is f21 First non-repeating character is f1 First non-repeating character is f23 First non-repeating character is f24 # Python program to print the first non-repeating character 6 First non-repeating character is f23 string First non-repeating character is f25 First non-repeating character is f1____208_______8
First non-repeating character is f8 First non-repeating character is f7 First non-repeating character is f1 string 2 First non-repeating character is f32
First non-repeating character is f2 First non-repeating character is f35 First non-repeating character is f36_______2_______37 First non-repeating character is f36_______2_______39 First non-repeating character is f40_______210_______1 First non-repeating character is f40_______2_______43
First non-repeating character is f36_______2_______4 = 6
First non-repeating character is f8 = 8First non-repeating character is f8 = 5= 6First non-repeating character is f1 = 8
First non-repeating character is f1 First non-repeating character is f01 First non-repeating character is f1 First non-repeating character is f59 First non-repeating character is f24 # Python program to print the first non-repeating character 6 First non-repeating character is f62 First non-repeating character is f1____208_______8
First non-repeating character is f8 string First non-repeating character is f67 "geeksforgeeks" = 6First non-repeating character is f8 First non-repeating character is f10 First non-repeating character is f1 = 8
First non-repeating character is f75 JavascriptFirst non-repeating character is f76
First non-repeating character is f77
First non-repeating character is f78 First non-repeating character is f79
First non-repeating character is f80 First non-repeating character is f7 First non-repeating character is f82 First non-repeating character is f80 # Python program to print the first non-repeating character 8
First non-repeating character is f85 First non-repeating character is f2 First non-repeating character is f87 First non-repeating character is f85 # Python program to print the first non-repeating character 8First non-repeating character is f90 First non-repeating character is f91
First non-repeating character is f90 First non-repeating character is f4 First non-repeating character is f85 = 8First non-repeating character is f80 = 8First non-repeating character is f80 = 5
First non-repeating character is f01 First non-repeating character is f02______2_______05 First non-repeating character is f06
First non-repeating character is f12
First non-repeating character is f06 Đầu ra First non-repeating character is f Thời gian phức tạp. O(N2) Ký tự không lặp lại đầu tiên sử dụng HashMap và hai lần duyệt chuỗi
|