给定一个数字num和 String str ,任务是通过使用数字的索引值从字符串提取字符来生成新的 String 。
例子:
Input: str = “GeeksforGeeks”
num = 858
Output: GfG
Explanation: The 8th, 5th, and 8th position of the characters are extracting from the given string and generate a new string.
Input: str = “Java”
num = 12
Output: The 1st and 2nd position of the characters are extracting from the given string and generate a new string.
方法 1:使用两次遍历
- 获取字符串和数字以生成新字符串。
- 初始化一个存储最终答案的变量。
- 将给定的数字转换为字符串。
- 从头到尾遍历循环。
- 获取数字的最后一位并将第 k 个位置字符添加到变量 answer 中。
- 以相反的顺序遍历循环并将第j 个位置字符添加到变量finalAnswer 中。
- 现在,打印结果。
Java
// Java program to generate String by using
// Strings and numbers
class GFG {
// Function to generate the new String
public static String generateNewString(String str,
int num)
{
// Initialize a variable that
// stores the final anser.
String finalAnswer = "";
String answer = "";
// Converting the given numbers
// into string
String index = String.valueOf(num);
// Traverse the loop from start to end
for (int i = 0; i < index.length(); i++) {
// Getting last digit of numbers
int k = num % 10;
// Adding kth position character
// into the variable answer
answer = answer + str.charAt(k);
num = num / 10;
}
// Traverse the loop in reverse order
for (int j = answer.length() - 1; j >= 0; j--) {
// Adding jth position character
// into the variable finalAnswer
finalAnswer = finalAnswer + answer.charAt(j);
}
// Return the finalAnswer
return finalAnswer;
}
// Driver Code
public static void main(String args[])
{
// Given String str
String str = "GeeksforGeeks";
// Given Number num
int num = 858;
// Printing the result
System.out.println(generateNewString(str, num));
}
}
Java
// Java program to generate String by using
// Strings and numbers
class GFG {
// Function to generate the new String
public static String generateNewString(String str,
int num)
{
// Initialize a variable that
// stores the result.
String result = "";
// Converting the given numbers
// into the string
String index = String.valueOf(num);
// Traverse the loop from start to end
for (int i = 0; i < index.length(); i++) {
// Getting the right index
int k = index.charAt(i) - 48;
// Adding kth position character
// into the result
result = result + str.charAt(k);
}
// Return result
return result;
}
// Driver Code
public static void main(String args[])
{
// Given String str
String str = "GeeksforGeeks";
// Given Number num
int num = 858;
// Printing the result
System.out.println(generateNewString(str, num));
}
}
输出
GfG
方法 2:使用一次遍历
- 获取字符串和数字以生成新字符串。
- 初始化一个存储结果的变量。
- 将给定的数字转换为字符串。
- 从头到尾遍历循环。
- 获取正确的索引并将第 k 个位置字符添加到结果中。
- 现在,打印结果。
Java
// Java program to generate String by using
// Strings and numbers
class GFG {
// Function to generate the new String
public static String generateNewString(String str,
int num)
{
// Initialize a variable that
// stores the result.
String result = "";
// Converting the given numbers
// into the string
String index = String.valueOf(num);
// Traverse the loop from start to end
for (int i = 0; i < index.length(); i++) {
// Getting the right index
int k = index.charAt(i) - 48;
// Adding kth position character
// into the result
result = result + str.charAt(k);
}
// Return result
return result;
}
// Driver Code
public static void main(String args[])
{
// Given String str
String str = "GeeksforGeeks";
// Given Number num
int num = 858;
// Printing the result
System.out.println(generateNewString(str, num));
}
}
输出
GfG
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live