给定一个由小写字母组成的字符串S。这项任务是找到仅具有相同长度的字典最小字符串X,该字符串可以使用下面给出的操作形成: 在单个操作中,从字符串S的最多前K个字符中选择任意一个字符,将其从字符串S中删除,并将其附加到字符串X。根据需要多次应用此操作。 例如:
null
输入: str=“gaurang”,k=3 输出: 阿甘鲁 在第一步中删除“a”并附加到X。 在第二步中删除“g”并附加到X。 在第三步中删除“a”并附加到X。 删除第三步中的“n”并附加到X。 在每一步从前K个字符中选取字典中最小的字符,以获得 字符串“agangru” 输入: str=“geeksforgeks”,k=5 输出: eefggeekkorss
方法:
- 在字符串S的前k个字符中找到最小的字符。
- 从字符串中删除找到的最小字符。
- 将找到的最小字符追加到新字符串X。
- 重复上述步骤,直到字符串s为空。
以下是上述方法的实施情况:
C++
// C++ program to find the new string // after performing deletions and append // operation in the string s #include <bits/stdc++.h> using namespace std; // Function to find the new string thus // formed by removing characters string newString(string s, int k) { // new string string X = "" ; // Remove characters until // the string is empty while (s.length() > 0) { char temp = s[0]; // Traverse to find the smallest character in the // first k characters for ( long long i = 1; i < k and i < s.length(); i++) { if (s[i] < temp) { temp = s[i]; } } // append the smallest character X = X + temp; // removing the lexicographically smallest // character from the string for ( long long i = 0; i < k; i++) { if (s[i] == temp) { s.erase(s.begin() + i); break ; } } } return X; } // Driver Code int main() { string s = "gaurang" ; int k = 3; cout << newString(s, k); } |
JAVA
// Java program to find the new string // after performing deletions and append // operation in the string s class GFG { // Function to find the new string thus // formed by removing characters static String newString(String s, int k) { // new string String X = "" ; // Remove characters until // the string is empty while (s.length() > 0 ) { char temp = s.charAt( 0 ); // Traverse to find the smallest character in the // first k characters for ( int i = 1 ; i < k && i < s.length(); i++) { if (s.charAt(i) < temp) { temp = s.charAt(i); } } // append the smallest character X = X + temp; // removing the lexicographically smallest // character from the string for ( int i = 0 ; i < k; i++) { if (s.charAt(i) == temp) { s = s.substring( 0 , i) + s.substring(i + 1 ); //s.erase(s.begin() + i); break ; } } } return X; } // Driver code public static void main(String[] args) { String s = "gaurang" ; int k = 3 ; System.out.println(newString(s, k)); } } // This code contributed by Jajput-Ji |
Python3
# Python 3 program to find the new string # after performing deletions and append # operation in the string s # Function to find the new string thus # formed by removing characters def newString(s, k): # new string X = "" # Remove characters until # the string is empty while ( len (s) > 0 ): temp = s[ 0 ] # Traverse to find the smallest # character in the first k characters i = 1 while (i < k and i < len (s)): if (s[i] < temp): temp = s[i] i + = 1 # append the smallest character X = X + temp # removing the lexicographically # smallest character from the string for i in range (k): if (s[i] = = temp): s = s[ 0 :i] + s[i + 1 :] break return X # Driver Code if __name__ = = '__main__' : s = "gaurang" k = 3 print (newString(s, k)) # This code is contributed by # Shashank_Sharma |
C#
// C# program to find the new string // after performing deletions and // append operation in the string s using System; class GFG { // Function to find the new string thus // formed by removing characters static String newString(String s, int k) { // new string String X = "" ; // Remove characters until // the string is empty while (s.Length > 0) { char temp = s[0]; // Traverse to find the smallest // character in the first k characters for ( int i = 1; i < k && i < s.Length; i++) { if (s[i] < temp) { temp = s[i]; } } // append the smallest character X = X + temp; // removing the lexicographically smallest // character from the string for ( int i = 0; i < k; i++) { if (s[i] == temp) { s = s.Substring(0, i) + s.Substring(i + 1); //s.erase(s.begin() + i); break ; } } } return X; } // Driver code public static void Main(String[] args) { String s = "gaurang" ; int k = 3; Console.Write(newString(s, k)); } } // This code contributed by Rajput-Ji |
PHP
<?php // PHP program to find the new string // after performing deletions and // append operation in the string s // Function to find the new string thus // formed by removing characters function newString( $s , $k ) { // new string $X = "" ; // Remove characters until // the string is empty while ( strlen ( $s ) > 0) { $temp = $s [0]; // Traverse to find the smallest // character in the first k characters for ( $i = 1; $i < $k && $i < strlen ( $s ); $i ++) { if ( $s [ $i ] < $temp ) { $temp = $s [ $i ]; } } // append the smallest character $X = $X . $temp ; // removing the lexicographically smallest // character from the string for ( $i = 0; $i < $k ; $i ++) { if ( $s [ $i ] == $temp ) { $s = substr ( $s , 0, $i ) . substr ( $s , $i + 1, strlen ( $s )); //s.erase(s.begin() + i); break ; } } } return $X ; } // Driver code $s = "gaurang" ; $k = 3; echo (newString( $s , $k )); // This code contributed by mits ?> |
Javascript
<script> // JavaScript program to find the new string // after performing deletions and // append operation in the string s // Function to find the new string thus // formed by removing characters function newString(s, k) { // new string var X = "" ; // Remove characters until // the string is empty while (s.length > 0) { var temp = s[0]; // Traverse to find the smallest // character in the first k characters for ( var i = 1; i < k && i < s.length; i++) { if (s[i] < temp) { temp = s[i]; } } // append the smallest character X = X + temp; // removing the lexicographically smallest // character from the string for ( var i = 0; i < k; i++) { if (s[i] === temp) { s = s.substring(0, i) + s.substring(i + 1); break ; } } } return X; } // Driver code var s = "gaurang" ; var k = 3; document.write(newString(s, k)); </script> |
输出:
agangru
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END