📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: 大聪明gyq
跳转到指定楼层
上一主题 下一主题
收起左侧

[找工就业] Uber 24ng oa

 
🔗
 楼主| 大聪明gyq 2024-6-29 11:16:09 | 只看该作者
全局:
liur 发表于 2024-6-28 22:50
我也拿到了一样的第四题,我的做法是prefix tree顺序一遍倒序一遍然后减去多数的一样的str。我觉得有更好的 ...

.. sorry,我刚看到。我问了问ChatGPT,大概这样:
import java.util.HashMap;
import java.util.Map;

public class SuffixPairChecker {
.1point3acres
    // Trie Node class
    static class TrieNode {
        Map<Character, TrieNode> children = new HashMap<>();.
        boolean isEndOfWord = false;
    }

. 1point 3acres     // Trie class
    static class Trie {
        TrieNode root = new TrieNode();
-baidu 1point3acres
        // Insert a word in reverse order
. .и        public void insert(String word) {. ----
            TrieNode node = root;
            for (int i = word.length() - 1; i >= 0; i--) {
                char c = word.charAt(i);
                node = node.children.computeIfAbsent(c, k -> new TrieNode());
            }. 1point 3 acres
            node.isEndOfWord = true;
        }

        // Search for a suffix in the trie
        public boolean searchSuffix(String word) {
            TrieNode node = root;
            for (int i = word.length() - 1; i >= 0; i--) {. From 1point 3acres bbs
                char c = word.charAt(i);
                if (!node.children.containsKey(c)) {
                    return false;
                }
                node = node.children.get(c);.--
                if (node.isEndOfWord) {
. Waral dи,                    return true;
                }
            }. 1point3acres
            return node.isEndOfWord;
        }
    }

    public int pairCheck(String[] words) {
        Trie trie = new Trie();
        int count = 0;

        // Insert all words into the trie in reversed order
        for (String word : words) {
            trie.insert(word);
        }

        // Check each word against the trie for suffix match
        for (String word : words) {
            // Remove the word from the trie to avoid counting itself. check 1point3acres for more.
            if (trie.searchSuffix(word)) {
                count++;
            }. .и
        }

        return count;
    }

    public static void main(String[] args) {
        SuffixPairChecker checker = new SuffixPairChecker();
        String[] words = {"apple", "ple", "banana", "na", "an"};
        int result = checker.pairCheck(words);
        System.out.println("Number of valid suffix pairs: " + result); // Expected output. 1point 3acres
    }
}
回复

使用道具 举报

🔗
moran_li 2024-6-29 22:29:30 | 只看该作者
全局:
q4
散岭死伞
toString and trie
回复

使用道具 举报

🔗
lilbird 2024-7-3 01:50:17 | 只看该作者
全局:
楼主有收到后续消息吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表