최대 고유 문자 수가 포함된 문자열
문자열 배열이 주어지면 작업은 다음과 같이 문자열을 인쇄하는 것입니다. 최대 수 독특한 캐릭터의.
메모:
- 문자열은 소문자로 구성됩니다.
- 문자열이 여러 개 있으면 그 중 하나를 인쇄합니다.
예:
입력: arr[] = ['abc' 'geeksforgeeks' 'gfg' '코드']
산출: "괴짜를 위한 괴짜"
설명: 'abc'에는 3개의 고유 문자가 있습니다. 'geeksforgeeks'에는 7개의 고유 문자가 있습니다.
'gfg'에는 3개의 고유 문자가 있습니다. 'code'에는 4개의 고유 문자가 있습니다.입력: arr[] = ['hello' '세계' '프로그래밍' '얼룩말']
산출: '프로그램 작성'
설명: '프로그래밍'에는 최대 8개의 고유 문자가 있습니다.
접근하다:
아이디어는 해당 고유 문자를 세고 지금까지 어떤 문자열이 가장 높은 개수를 생성했는지 추적하는 부울 배열을 사용하여 어떤 소문자가 나타나는지 추적하는 각 문자열을 체계적으로 검사하는 것입니다.
단계별 접근 방식:
- 컬렉션의 각 문자열을 반복합니다.
- 각 문자열에 대해 부울 배열을 사용하여 어떤 문자가 있는지 표시합니다.
- 현재로 표시된 고유 문자 수를 세어보세요.
- 이 개수가 이전 최대값을 초과하면 최대값을 업데이트하고 이 문자열의 위치를 기억하세요.
- 발견된 고유 문자 수가 가장 많은 문자열을 반환합니다.
// C++ code to find string with maximum // number of unique characters. #include using namespace std ; // Function to find string with // maximum number of unique characters. string maxString ( vector < string > & arr ) { int n = arr . size (); int index = -1 maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { vector < bool > map ( 26 false ); for ( int j = 0 ; j < arr [ i ]. length (); j ++ ) { map [ arr [ i ][ j ] - 'a' ] = true ; } // Find number of unique char int cnt = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( map [ j ] == true ) cnt ++ ; } // If unique count is greater if ( cnt > maxCnt ) { maxCnt = cnt ; index = i ; } } return arr [ index ]; } int main () { vector < string > arr = { 'abc' 'geeksforgeeks' 'gfg' 'code' }; cout < < maxString ( arr ); return 0 ; }
Java // Java code to find string with maximum // number of unique characters. import java.util.* ; class GfG { // Function to find string with // maximum number of unique characters. static String maxString ( String [] arr ) { int n = arr . length ; int index = - 1 maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { boolean [] map = new boolean [ 26 ] ; for ( int j = 0 ; j < arr [ i ] . length (); j ++ ) { map [ arr [ i ] . charAt ( j ) - 'a' ] = true ; } // Find number of unique char int cnt = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( map [ j ] == true ) cnt ++ ; } // If unique count is greater if ( cnt > maxCnt ) { maxCnt = cnt ; index = i ; } } return arr [ index ] ; } public static void main ( String [] args ) { String [] arr = { 'abc' 'geeksforgeeks' 'gfg' 'code' }; System . out . println ( maxString ( arr )); } }
Python # Python code to find string with maximum # number of unique characters. # Function to find string with # maximum number of unique characters. def maxString ( arr ): n = len ( arr ) index = - 1 maxCnt = 0 for i in range ( n ): map = [ False ] * 26 for j in range ( len ( arr [ i ])): map [ ord ( arr [ i ][ j ]) - ord ( 'a' )] = True # Find number of unique char cnt = sum ( 1 for j in range ( 26 ) if map [ j ]) # If unique count is greater if cnt > maxCnt : maxCnt = cnt index = i return arr [ index ] if __name__ == '__main__' : arr = [ 'abc' 'geeksforgeeks' 'gfg' 'code' ] print ( maxString ( arr ))
C# // C# code to find string with maximum // number of unique characters. using System ; class GfG { // Function to find string with // maximum number of unique characters. static string maxString ( string [] arr ) { int n = arr . Length ; int index = - 1 maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool [] map = new bool [ 26 ]; for ( int j = 0 ; j < arr [ i ]. Length ; j ++ ) { map [ arr [ i ][ j ] - 'a' ] = true ; } // Find number of unique char int cnt = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( map [ j ] == true ) cnt ++ ; } // If unique count is greater if ( cnt > maxCnt ) { maxCnt = cnt ; index = i ; } } return arr [ index ]; } static void Main () { string [] arr = { 'abc' 'geeksforgeeks' 'gfg' 'code' }; Console . WriteLine ( maxString ( arr )); } }
JavaScript // JavaScript code to find string with maximum // number of unique characters. // Function to find string with // maximum number of unique characters. function maxString ( arr ) { let n = arr . length ; let index = - 1 maxCnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let map = new Array ( 26 ). fill ( false ); for ( let j = 0 ; j < arr [ i ]. length ; j ++ ) { map [ arr [ i ]. charCodeAt ( j ) - 'a' . charCodeAt ( 0 )] = true ; } // Find number of unique char let cnt = 0 ; for ( let j = 0 ; j < 26 ; j ++ ) { if ( map [ j ] === true ) cnt ++ ; } // If unique count is greater if ( cnt > maxCnt ) { maxCnt = cnt ; index = i ; } } return arr [ index ]; } let arr = [ 'abc' 'geeksforgeeks' 'gfg' 'code' ]; console . log ( maxString ( arr ));
산출
geeksforgeeks
시간 복잡도: 오(n*m) 어디 N 주어진 문자열 배열의 크기이고 중 주어진 배열에 있는 문자열의 가장 큰 크기입니다.
보조 공간: O(1)