给定一个由空格分隔的多个单词组成的字符串str ,任务是打印该字符串的垂直顺序遍历。垂直顺序遍历是指当每个单词按垂直顺序插入网格时,对给定字符串形成的网格的行遍历。
注意:空格(“”)由“ $ ”符号表示,以便更好地可视化。
例子:
Input: str = “geeks for geeks”
Output: [gfg, eoe, ere, k$k, s$s]
Explanation: Vertical order of the string will result in the following column values.
g f g
e o e
e r e
k $ k
s $ s
Now the Answer will be a list of strings appearing row-wise.
Input: str = “ge ek sfor ge eks”
Output: [gesge, ekfek, $$o$s, $$r$$]
方法:想法是使用HashMap,其中hashmap的键代表索引,值代表该索引处的字符。可以按照以下步骤计算垂直顺序遍历。
- 将给定的字符串拆分为单词。
例如:
If String is "Geeks For Geeks"
then the words will be
words = {"Geeks", "For", "Geeks"}
- 最初,找到最大长度的单词。然后所有的单词都小于或等于这个长度。
- 然后创建一个最大单词大小的HashMap。
例如:
If the maximum length word is 5,
then the HashMap will be
map = {
{0, ""},
{1, ""},
{2, ""},
{3, ""},
{4, ""}
}
- 对于每个单词,然后检查该单词是否具有此最大长度。如果是,则将其附加到 HashMap。否则,额外的空格(表示为“ $ ”)被添加到字符串。
例如:
For String = "Geeks For Geeks"
then the HashMap after word "Geeks"
map = {
{0, "G"},
{1, "e"},
{2, "e"},
{3, "k"},
{4, "s"}
}
then the HashMap after word "For"
map = {
{0, "GF"},
{1, "eo"},
{2, "er"},
{3, "k$"},
{4, "s$"}
}
then the HashMap after word "Geeks"
map = {
{0, "GFG"},
{1, "eoe"},
{2, "ere"},
{3, "k$k"},
{4, "s$s"}
}
- HashMap 中每个索引处的单词表示给定字符串的最终垂直顺序遍历中的单词。
- 因此,最后,从 HashMap 中获取(从每个索引)字符串并添加到数组中,然后返回数组。
例如:
Vertial order traversal of the above example:
"GFG eoe ere k$k s$s"
下面是上述方法的实现:
Java
// Java implementation of the above approach.
import java.util.*;
class GFG {
// Function to return the vertical order
// traversal of String
public static String printVertically(String s)
{
String result = "";
// Hashmap to store the indices
// of the characters
HashMap map
= new HashMap<>();
Scanner sc = new Scanner(s);
int max = Integer.MIN_VALUE;
// Finding maximum length word
// from given string
while (sc.hasNext()) {
String st = sc.next();
max = Math.max(max, st.length());
}
sc = new Scanner(s);
while (sc.hasNext()) {
String st = sc.next();
// Inserting strings in the hashmap
// in vertical fashion
// based on character index
for (int i = 0; i < st.length(); i++) {
char ch = st.charAt(i);
map.put(i, map.getOrDefault(i, "") + ch);
}
// If the string is smaller than the
// maximum length string, then
// add spaces in the string
for (int i = st.length(); i < max; i++) {
map.put(i,
map.getOrDefault(i,
"")
+ "$");
}
}
// Adding all String values in the list
for (int i = 0; i < max; i++) {
String a = map.get(i);
result += a + " ";
}
return result;
}
// Driver code
public static void main(String[] args)
{
String st = "ge ek sfor ge eks";
System.out.println(printVertically(st));
}
}
Python3
# Python3 implementation of the above approach.
# Function to return the vertical order
# traversal of
def printVertically(s):
s = s.split(" ")
result = ""
# Hashmap to store the indices
# of the characters
map = dict()
mx = 0
# Finding maximum length word
# from given string
for i in s:
mx = max(mx, len(i))
n = mx
i = 0
while (i < mx):
# Inserting strings in the hashmap
# in vertical fashion
# based on character index
for j in s:
if i >= len(j):
map[i] = map.get(i, "") + "$"
else:
map[i] = map.get(i, "") + j[i]
i += 1
# Adding all values in the list
for j in range(mx):
result += map[j] + " "
return result
# Driver code
if __name__ == '__main__':
st = "ge ek sfor ge eks"
print(printVertically(st))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the above approach.
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the vertical order
// traversal of String
public static String printVertically(String s)
{
String result = "";
// Hashmap to store the indices
// of the characters
Dictionary map
= new Dictionary();
String []sc = s.Split(' ');
int max = int.MinValue;
int i = 0;
// Finding maximum length word
// from given string
while (i
Javascript
输出:
gesge ekfek $$o$s $$r$$
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。